Queue
Queue
“What is a queue?”
A queue is a special type of data structure (an ordered collection of items) where elements are
inserted from one end and elements are deleted from the other end. The end at which new elements
are added is called the rear and the end from which elements are deleted is called the front. Using
this approach, the First element Inserted is the First element to be deleted Out, and hence, queue is
also called First In First Out (FIFO) data structure.
Based on the method of insertion and deletion the queues are classified as shown below:
1. Linear queue (Ordinary queue)
2. Circular queue
3. Double ended queue (dequeue or qeque)
4. Priority queue
For example, consider the queue shown below having the elements 10, 50 and 20:
The items are inserted into queue in the order 10, 50 and 20. The variable q is used as an array
to hold these elements
Item 10 is the first element inserted. So, the variable first is used as index to the first element
Item 20 is the last element inserted. So, the variable rear is used as index to the last element
Two more items can be inserted into above queue.
Operations on queue
A queue (linear queue or ordinary queue) is a special type of data structure (an ordered collection of
items) where elements are inserted from one end and elements are deleted from the other end. The
end at which new elements are added is called the rear and the end from which elements are deleted
is called the front. Using this approach, the First element Inserted is the First element to be deleted
out, and hence, queue is also called First In First Out (FIFO) data structure.
For example, consider the queue shown below having the elements 10, 50 and 20:
The items are inserted into queue in the order 10, 50 and 20. The variable q is
used as an array to hold these elements
Item 10 is the first element inserted. So, the variable first is used as index to the
first element
Item 20 is the last element inserted. So, the variable rear is used as index to the
last element
Two more items can be inserted into above queue.
1 Insert into queue
Step 1: Consider the queue shown below. Can we insert any element into the queue?
No, it is not possible because queue is full.
Observe that whenever rear value is
equal to “QUEUE_SIZE – 1” insertion is
not possible. The code for this can be
written as shown below:
if (rear == QUEUE_SIZE - 1)
{
printf(“Queue is full\n”);
return;
}
Now, the complete function to insert an element from the rear end of the queue can
be written as shown below:
void Insert_Rear()
{
/* Check for overflow of queue */
if (rear == QUE_SIZE – 1)
{
printf(“Queue overflow\n”);
return;
}
/* Insert the item */
rear = rear + 1;
q[rear] = item;
}
Step 1: Now, let us see “How to check whether queue is empty or not?”
So, it is observed from above two figures that if front is less than or equal to rear
some elements are present. Otherwise, that is, if front is greater than rear then queue
is empty. We can check for empty queue using the following statement:
if (front > rear) return -1; // Que is empty
Step 2: When above condition fails, we can delete an item from front end of queue.
For this to happen, we have to access and return the first element and increment value
of front by 1 as shown below:
return q[front++];
Now, the complete function to delete an element from the front end of the queue can
be written as shown below:
int Delete_Front()
{
if (front > rear) return -1;
return q[front++];
}
Note: The variables front, rear and q are global. So, they can be accessed in all the functions.
Types of Queues
2. Circular queue
In circular queue, the elements of a given queue can be stored efficiently in an array so as to “wrap
around” so that rear end of the queue is followed by the front of queue. The pictorial representation of
a circular queue and its equivalent representation using an array are given side by side in figure below:
4. Priority queue
A queue in which we are able to insert items or remove items from any
position based on some priority is often referred to as a priority queue. Always an
element with highest priority is processed before processing any of the lower priority
elements. If the elements in the queue are of same priority, then the element, which
is
inserted first into the queue, is processed. Priority queues are used in job scheduling
algorithms in the design of operating system where the jobs with highest priorities
have to be processed first.
The priority queues are classified into two groups:
Ascending priority queue: In an ascending priority queue elements can be
inserted
in any order. But, while deleting an element from the queue, only the smallest
element is removed first.
Descending priority queue: In descending priority also elements can be
inserted in
any order. But, while deleting an element from the queue, only the largest element
is deleted first.