2. Queue
Definition:-A queue is another special kind of list, where items are
inserted at one end called the rear and deleted at the other end
called the front. Another name for a queue is a “FIFO” or “First-in-
first-out” list.
Example:-Queue of people who are waiting for city
Bus at bus stop.
Primary queue operations:
• Enqueue: insert an element at the rear of the queue
• Dequeue: remove an element from the front of the queue
Applications of Queue:
1. It is used to schedule the jobs to be processed by the CPU.
2. When multiple users send print jobs to a printer, each printing job
is kept in the printing queue. Then the printer prints those jobs
according to first in first out (FIFO) basis.
3. Breadth first search uses a queue data structure to find an element
from a graph.
3. Let us consider a queue, which can hold maximum of Six elements. Initially the queue
is empty.
Now it is not possible to insert an element 66 (rear cross the max size means queue
overflow)even though there are two vacant positions in the linear queue. To over come
this problem .we discuss circular Queue.
6. Circular Queue
Circular queue is implemented by making a array of queue
circular. This implementation of a queue is called a
circular queue because it uses its storage array as if it
were a circle instead of a linear list.
There are two problems associated with linear queue. They
are:
• Time consuming: linear time to be spent in shifting the
elements to the beginning of the queue.
• Signaling queue full: even if the queue is having vacant
position.
7. Let us consider a queue of size 6(queue[5]),initialy front =-1 & rear =-1
12. C implementation of circular queue
# define max 5
int rear=-1;
int front=-1;
int queue[max];
void queue_insert()
{
int item;
if(front==(rear+1)%max)
{
printf("nYour queue is full");
}
else
{ printf("nEnter the item:");
scanf("%d",&item);
if(front==-1)
{ rear=0;front=0;
queue[rear]=item;
}
else
{
rear=(rear+1)%max;
queue[rear]=item;
void queue_delete()
{
int item;
if(front==-1)
{
printf("nYour queue is empty");
}
else
{
item=queue[front];
if(front==rear)
{
front=-1; rear=-1;
printf("nYour deleted item is:%d",item);
}
else
{
front=(front+1)%max;
printf("nYour deleted item is:%d",item);
}
}
}
13. void display()
{ int i;
if(front== -1)
{
printf("nYour queue is empty");
}
else
{
if(front>rear)
{
for(i=front;i<max;i++)
{
printf("nqueue is:%d",queue[i]);
}
for(i=0;i<=rear;i++)
{
printf("nqueue is:%d",queue[i]);
}
}
else
{
for(i=front;i<=rear;i++)
{ printf("nqueue is:%d",queue[i]);
}}} }
void main()
{ int choice;
char ch;
clrscr();
do
{
printf("nEnter the option:");
printf("n1.Insert");
printf("n2.Delete");
printf("n3.Display");
printf("nYour choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: queue_insert();
break;
case 2: queue_delete();
break;
case 3: display();
break;
}
printf("nIf you want to continue press Y:");
scanf("%c",&ch);
}while(ch=='y');
getch();}
scanf
14. DEQUEUE (OR) DEQUE (DOUBLE ENDED QUEUE)
• DeQueue is a data structure in which elements may be added to or deleted from the
front or the rear.
• It support the following operation:-
insert from front
delete from front
insert from rear
delete from rear
15. int front=-1;
int rear=-1;
#define max 5
int dequeue[max];
#include<stdio.h>
#include<conio.h>
void insert_rear()
{
int item;
if(rear==max-1)
printf("nYour Queue is Full");
else
{
printf("nEnter the item:");
scanf("%d",&item);
if(rear==-1)
{
front=0; rear=0;
dequeue[rear]=item;
}
else
{
rear=rear+1;
dequeue[rear]=item;
} } }
void delete_rear()
{
int item;
if(front==-1)
printf("nYour Dequeue is empty");
else
{
if(rear==front)
{
item=dequeue[rear];
rear=-1;front=-1;
printf("nYour deleted item is:%d",item);
}
else
{
item=dequeue[rear];
rear=rear-1;
printf("nYour deleted item is:%d",item);
}
}
}
16. void insert_front()
{
int item;
if(front==0)
printf("nYour queue is full");
else
{
printf("nEnter your item:");
scanf("%d",&item);
front=front-1;
dequeue[front]=item;
}
}
void display()
{ int i;
for(i=front;i<=rear;i++)
{
printf("nYour queue is %d",dequeue[i]);
}
}
void delete_front()
{
int item;
if(front==-1)
printf("nYour dequeue is empty");
else
{
if(front==rear)
{
item=dequeue[front];
front=-1;rear=-1;
printf("nYour delelted item is:%d",item);
}
else
{
item=dequeue[front];
front=front+1;
printf("nYour deleted item is:%d",item);
}
}
}
17. void main()
{
int choice;
char ch;
do
{
printf("nChoose the option:");
printf("n1.Insert Rear");
printf("n2.Delete Rear");
printf("n3.Insert Front");
printf("n4.Delete Front");
printf("n5.Display");
printf("n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert_rear();
break;
case 2: delete_rear();
break;
case 3: insert_front();
break;
case 4: delete_front();
break;
case 5: display();
break;
}
printf("nIf you want to continue press y:");
scanf("%s",&ch);
}while(ch=='y');
getch();
}
18. Priority Queue:
A priority queue is a collection of elements such that each element has been assigned
a priority and such that the order in which elements are deleted and processed
comes from the following rules:
1. An element of higher priority is processed before any element of lower
priority.
2. two elements with same priority are processed according to the order in
which they were added to the queue.
A prototype of a priority queue is time sharing system: programs of high priority are
processed first, and programs with the same priority form a standard queue. An
efficient implementation for the Priority Queue is to use heap, which in turn can be
used for sorting purpose called heap sort.