A Short Explained and Clean Code For Queue
Definition: A queue is an ordered list in which insertions are done at one end (rear) and deletions are done at other end (front). The first element to be inserted is the first one to be deleted. Hence, it is called First in First out (FIFO) or Last in Last out (LILO) list.
Similar to Stacks, special names are given to the two changes that can be made to a queue. When an element is inserted in a queue, the concept is called EnQueue, and when an element is removed from the queue, the concept is called DeQueue. DeQueueing an empty queue is called underflow and EnQueuing an element in a full queue is called overflow.
Main Queue Operations
- EnQueue(int data): Inserts an element at the end of the queue .
- int DeQueue(): Removes and returns the element at the front of the queue .
Auxiliary Queue Operations
- int Front(): Returns the element at the front without removing it .
- int QueueSize(): Returns the number of elements stored in the queue .
- int IsEmptyQueueQ: Indicates whether no elements are stored in the queue or not .
Direct Applications
- Operating systems schedule jobs (with equal priority) in the order of arrival (e.g., a print queue).
- Simulation of real-world queues such as lines at a ticket counter or any other first- come first-served scenario requires a queue.
- Multiprogramming.
- Asynchronous data transfer (file IO, pipes, sockets).
- Waiting times of customers at call center.
- Determining number of cashiers to have at a supermarket.
There are many ways (similar to Stacks) of implementing queue operations and some of the commonly used methods are listed below.
- Simple circular array based implementation
- Dynamic circular array based implementation
- Linked list implementation