Queue is an ordered list in which all insertions at one end called REAR and deletions are made at another end called FRONT. Queues are sometimes referred to as First In First Out (FIFO) list.
Examples of Queue:
People waiting in line at the bank queue counter from a queue.
In computer, the jobs waiting in line to use processor for execution, this queue is known as Job Queue.
Operations of Queue:
There are two basic queue operations:
- Enqueue – It inserts item or element at the rear of the queue. An error occurs if the queue is full.
- Dequeue – It removes or deletes an item or element from the front end of the queue, and returns to the user. An error occurs if the queue is empty.
Algorithm to insert an item into Queue:
procedure insertq (item : items) ; { add item to the queue q } begin if rear = n then queue is full else begin rear := rear + 1 ; q[rear] := item ; end ; end ; (of insert q)
Algorithm to delete an item from Queue:
procedure deleleq (var item : items) ; { delete from the front of q and return to user } begin if front = rear then queue is empty else begin item := q[front] ; front := front + 1 ; end ; end ; (of deletion)
Application of Queue:
Queue uses First In First Out (FIFO) order, which is good for fair (FIFO) ordering of actions. Application of Queues are:
- Scheduling processing of GUI events printing request.
- Simulation orders event models real life queue, i.e., Supermarkets checkout, phone call on hold, etc.
You can watch these two nice videos on Queue Data Structure.
Part-1:
Part-2:
If you would like to contribute, you can also mail your article to raju.cplusplus@gmail.com. Please suggest your improvement for this article if you find anything incorrect.