A QUEUE is a list with the restriction that insertion is done at one end , where as deletion at the other and.for that reason it is called FIFO ( First in first out ) structure.
The end where insertion is performed is called the rear .
The end where Deletion is performed is called the front .
The number of elements in a queue is called the size of the queue
Set of operations:
Enqueue : Inserts an element at the end (rear) of the list
Dequeue : Returns and deletes the element at the start (front) of the list.
Define an Array â€˜ Queue â€™ of sufficient capacity to store Queue elements.
Uses a variable i.e rear to remember the position of insertion end.
Uses a variable i.e front to remember the position of deletion end.
Uses a variable i.e size to remember the size of the array.
Write a function Enqueue which takes an element x as argument and performs
size = size +1
rear = rear +1
Queue[rear] = x
Write a function Dequeue, which returns the value at front as
return Queue[front]
front=front+1
size=size-1
5.
Example: 4 2 R F Size = 2 Initial 4 2 1 F Size = 3 R After Enqueue(1) 4 2 3 1 F Size = 4 R After Enqueue(3) 4 2 3 1 F Size = 3 R After Dequeue r, 2 is returned 4 2 3 1 Size = 2 R F After Dequeue, 4 is returned 4 2 3 1 Size = 1 RF After Dequeue, 1 is returned 4 2 3 1 Size = 0 F R After Dequeue, 3 is returned