Page 192 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 192
Unit 11: Operations and Applications of Queues
int i; Notes
if (front == -1)
printf(“Queue is empty\n”);
else
{
printf(“Queue is :\n”);
for(i=front;i<= rear;i++)
printf(“%d “,queue[i]);
printf(“\n”);
}
}
Let us now consider the following example of using the operation Enqueue and Dequeue.
Example: A queue of an array A[3] is initialized and now the size of the queue is 3 which
represents the queue can hold a maximum of 3 items. The enqueue operation is used to add an
item to the queue.
Enqueue (3) – This will add an item called 3 to the queue in the front end. (Front count will be
incremented by 1).
Again we are adding another item to the queue.
Enqueue (5) – This will add an item called 5 to the queue (Front count will be incremented by 1)
Again we are adding the last item to the queue.
Enqueue (8) – This will add an item called 8 to the queue (Now the queue has reached its
maximum count and hence no more addition of an item is possible in the queue, as the queue has
reached the rear end).
Now dequeue operation is performed to remove an item from the queue.
Dequeue () – This will remove the item that has been added first in the queue, i.e., the item called
3 by following the concept of FIFO. Now the queue consists of the remaining items 5 and 8 in
which the object 5 will be in the front end. The dequeue operation continues until the queue is
reaching its last element.
Task Illustrate the working of dequeue operation with example.
Self Assessment
Fill in the blanks:
1. Queue is a data structure that maintain .......................... order.
2. Queue is usually used as a data structure for ..........................
3. .......................... operation checks whether the queue is empty.
4. .......................... operation checks whether the queue is full.
5. The primitive is Empty(Q) is required to know whether the queue is empty or not, because
calling next on an empty queue should cause an ..........................
LOVELY PROFESSIONAL UNIVERSITY 185