Queue
Queue
A queue is a FIFO (First In First Out) or LILO (Last In Last Out) data structure in
which items are inserted at one end called REAR end or the tail of the queue
whereas items are deleted from another end called FRONT end or the head of the
queue.
In other words, it can be defined as a list or a collection with a constraint that the
insertion can be performed at one end called the rear end or tail of the queue and
deletion is performed on another end called the front end or the head of the queue.
A queue is used in such a situation when we want to retrieve items in the same
order as they were inserted.
o Enqueue(): When we insert an item into the queue then the operation is
known as enqueue. If the queue is full then the overflow error occurs.
o Dequeue(): When we delete an item from the queue, the operation is known
as dequeue. If the queue is empty than the underflow error occurs.
o isFull(): It determines whether the queue is full or not.
o isEmpty(): It determines whether the queue is empty or not.
o Peek(): It returns the front item of queue without removing it.
o Display(): It prints all the items available in the queue.
A Queue can be represented as a container opened from both the sides in
which the element can be enqueued from one side and dequeued from
another side
Applications of Queue
Due to the fact that queue performs actions on first in first out basis which is quite
fair for the ordering of actions. There are various applications of queues discussed
as below.
Lines at a ticket counter or any other first come first served scenario
requires a queue.
Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for ex. pipes,
file IO, sockets.
Types of Queue
ENQUEUE(queue,front,rear,item,MAXSIZE)
If front = -1 then
else
[End of If structure]
DEQUEUE(queue,front,rear)
int queue[MAXSIZE];
int front=-1,rear=-1;
int isFULL(void)
{
if(rear==MAXSIZE-1)
return 1;
return 0;
}
int isEMPTY(void)
{
if(front==-1)
return 1;
return 0;
}
int DEQUEUE(void)
{
int delitem;
if(isEMPTY())
{
printf("Queue underflow error.");
return -1;
}
delitem=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
return delitem;
}
int PEEK(void)
{
if(isEMPTY())
{
printf("Queue underflow error.");
return -1;
}
return queue[front];
}
void DISPLAY(void)
{
int i;
if(isEMPTY())
printf("Queue is empty.");
else
{
printf("Items of Queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}
int main()
{
int item,choice;
while(1)
{
switch(choice)
{
case 1:
printf("\nEnter the item:");
scanf("%d",&item);
ENQUEUE(item);
break;
case 2:
item=DEQUEUE();
if(item!=-1)
printf("\n%d is deleted from Queue",item);
break;
case 3:
item=PEEK();
if(item!=-1)
printf("\nItem at Front of Queue = %d",item);
break;
case 4:
DISPLAY();
break;
case 5:
printf("\n\n\t\t\t_ _ _ Good Bye _ _ _");
exit(0);
default:
printf("\nWrong choice.");
}
}
return 0;
}
If we delete some items from queue then rear still remains set at MAXSIZE-1 so we
can’t insert more items even though the space is available in linear queue.
Insertion in Circular Queue
Algorithm
ENQUEUE(queue,front,rear,item,MAXSIZE)
If front = -1 then
else
[End of If structure]
DEQUEUE(queue,front,rear,MAXSIZE)
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
int queue[MAXSIZE];
int front=-1,rear=-1;
int isFULL(void)
if((front==0&&rear==MAXSIZE-1)||(front==rear+1))
return 1;
return 0;
int isEMPTY(void)
if(front==-1)
return 1;
return 0;
{
if(isFULL())
else
if(isEMPTY())
front=rear=0;
else
rear=(rear+1)%MAXSIZE;
queue[rear]=item;
int DEQUEUE(void)
int delitem;
if(isEMPTY())
return -1;
delitem=queue[front];
if(front==rear)
front=rear=-1;
else
front=(front+1)%MAXSIZE;
return delitem;
int PEEK(void)
if(isEMPTY())
return -1;
return queue[front];
void DISPLAY(void)
int i;
if(isEMPTY())
printf("\nQueue is empty.");
return;
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
else
for(i=front;i<=MAXSIZE-1;i++)
printf("%d\t",queue[i]);
for(i=0;i<=rear;i++)
printf("%d\t",queue[i]);
int main()
int item,choice;
while(1)
printf("\n\n\t\t\t1. INSERTION");
printf("\n\n\t\t\t2. DELETION");
printf("\n\n\t\t\t3. PEEK");
printf("\n\n\t\t\t4. DISPLAY");
printf("\n\n\t\t\t5. EXIT");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&item);
ENQUEUE(item);
break;
case 2:
item=DEQUEUE();
if(item!=-1)
break;
case 3:
item=PEEK();
if(item!=-1)
break;
case 4:
DISPLAY();
break;
case 5:
exit(0);
default:
printf("\nWrong choice.");
return 0;
Deque can be used both as stack and queue as it allows the insertion and deletion
operations on both ends.
The stack follows the LIFO rule in which both the insertion and deletion can be
performed only from one end. In deque, the insertion and deletion operation can be
performed from one side. Therefore, we conclude that deque can be
considered as a stack.
The queue follows the FIFO rule in which the element is inserted on one end and
deleted from another end. In deque, the insertion can be performed on one end,
and the deletion can be done on another end. Therefore, we conclude that the
deque can also be considered as the queue.
1. Input-restricted queue
The input-restricted queue means that some restrictions are applied to the
insertion. In input-restricted queue, the insertion is applied to one end while
the deletion is applied from both the ends.
2. Output-restricted queue
The output-restricted queue means that some restrictions are applied to the
deletion operation. In an output-restricted queue, the deletion can be applied
only from one end, whereas the insertion is possible from both ends.
Operations on Deque
The following operations can be applied on deque:
o Insert at front
o Delete from front
o Insert at rear
o Delete from rear
Other than insertion and deletion, we can also perform peek operation in deque.
Through peek operation, we can get the front and the rear element of the
dequeue.
Applications of Deque
The deque can be used as a stack and queue; therefore, it can perform
both redo and undo operations.
It can be used as a palindrome checker means that if we read the string
from both ends, then the string would be the same.
ENQUEUE_REAR(queue,front,rear,item,MAXSIZE)
If front = -1 then
else
[End of If structure]
ENQUEUE_FRONT(queue,front,rear,item,MAXSIZE)
If front = -1 then
[End of If structure]
DEQUEUE_FRONT(queue,front,rear,MAXSIZE)
DEQUEUE_REAR(queue,front,rear,MAXSIZE)
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
int queue[MAXSIZE];
int front=-1,rear=-1;
int isFULL(void)
if((front==0&&rear==MAXSIZE-1)||(front==rear+1))
return 1;
return 0;
int isEMPTY(void)
if(front==-1)
return 1;
return 0;
if(isFULL())
if(isEMPTY())
front=rear=0;
else
rear=(rear+1)%MAXSIZE;
queue[rear]=item;
if(isFULL())
else
if(isEMPTY())
front=rear=0;
else
front=(front+MAXSIZE-1)%MAXSIZE;
queue[front]=item;
}
}
int DEQUEUE_FRONT(void)
int delitem;
if(isEMPTY())
return -1;
delitem=queue[front];
if(front==rear)
front=rear=-1;
else
front=(front+1)%MAXSIZE;
return delitem;
int DEQUEUE_REAR(void)
int delitem;
if(isEMPTY())
return -1;
delitem=queue[rear];
if(front==rear)
front=rear=-1;
else
rear=(rear+MAXSIZE-1)%MAXSIZE;
return delitem;
int PEEK_FRONT(void)
if(isEMPTY())
return -1;
return queue[front];
}
int PEEK_REAR(void)
if(isEMPTY())
return -1;
return queue[rear];
void DISPLAY(void)
int i;
if(isEMPTY())
printf("\nQueue is empty.");
return;
for(i=front;i!=rear;i=(i+1)%MAXSIZE)
printf("%d\t",queue[i]);
printf("%d",queue[i]);
int main()
int item,choice;
while(1)
printf("\n\n\t\t\t5. PEEK(Front)");
printf("\n\n\t\t\t6. PEEK(Rear)");
printf("\n\n\t\t\t7. DISPLAY");
printf("\n\n\t\t\t8. EXIT");
scanf("%d",&choice);
switch(choice)
case 1:
ENQUEUE_REAR(item);
break;
case 2:
scanf("%d",&item);
ENQUEUE_FRONT(item);
break;
case 3:
item=DEQUEUE_FRONT();
if(item!=-1)
break;
case 4:
item=DEQUEUE_REAR();
if(item!=-1)
break;
case 5:
item=PEEK_FRONT();
if(item!=-1)
case 6:
item=PEEK_REAR();
if(item!=-1)
break;
case 7:
DISPLAY();
break;
case 8:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\nWrong choice.");
return 0;
}
Priority Queue
A priority queue is a data structure in which each element is assigned a priority. The
priority of the element will be used to determine the order in which the elements
will be processed. The general rules of processing the elements of a priority queue
are
In real word example when a VIP comes for a service, he will get the service before
the other people standing in a Queue because he has the priority over other people.
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.
In descending priority queue, also elements can be inserted in any order. But, while
deleting an element from the queue, only the largest element is deleted first.
ENQUEUE(queue,front,rear,item,MAXSIZE)
queue[rear] = item
else
set j = rear
set j = j-1
set queue[j+1]=item
[End of If structure]
Step 3: return
DEQUEUE(queue,front,rear)
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
int queue[MAXSIZE];
int front=-1,rear=-1;
int isFULL(void)
if(rear==MAXSIZE-1)
return 1;
return 0;
int isEMPTY(void)
if(front==-1)
return 1;
return 0;
int j;
if(isFULL())
else
if(isEMPTY())
front=rear=0;
queue[rear]=item;
else
j=rear;
while(j>=front&&item<queue[j])
queue[j+1]=queue[j];
j=j-1;
queue[j+1]=item;
rear=rear+1;
}
int DEQUEUE(void)
int delitem;
if(isEMPTY())
return -1;
delitem=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
return delitem;
int PEEK(void)
if(isEMPTY())
return -1;
}
return queue[front];
void DISPLAY(void)
int i;
if(isEMPTY())
printf("Queue is empty.");
else
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
int main()
int item,choice;
while(1)
{
printf("\n\t\t\t_ _ _ _ OPERATIONS ON PRIORITY QUEUE _ _ _ _\n");
printf("\n\n\t\t\t1. INSERTION");
printf("\n\n\t\t\t2. DELETION");
printf("\n\n\t\t\t3. PEEK");
printf("\n\n\t\t\t4. DISPLAY");
printf("\n\n\t\t\t5. EXIT");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&item);
ENQUEUE(item);
break;
case 2:
item=DEQUEUE();
if(item!=-1)
break;
case 3:
item=PEEK();
if(item!=-1)
printf("\nItem at Front of Queue = %d",item);
break;
case 4:
DISPLAY();
break;
case 5:
exit(0);
default:
printf("\nWrong choice.");
return 0;