Module - 2 QUEUE
Module - 2 QUEUE
Unit – II
QUEUE
Syllabus:
Queues: Definition, Operations, Implementation using Arrays.
Types of queues: Circular queue, Deque and priority queue.
Applications of queues: Message queue using circular queue
Queue is a linear non primitive data structure in which insertion are made
through one end and deletion are made through other end, according to First In
First Out (FIFO) principles.
Types of queue
1. Ordinary queue
2. Circular queue
3. Double ended queue
4. Priority queue
In ordinary queue the insertion of elements through rear end and deletion of
elements from the front end.
1. Initial assume that rear =-1 and front =0 indicates the queue is empty
-1 0 1 2 3
Rear Front
Figure1 represents the queue is empty
Rear/Front
Front Rear
4. We can insert maximum 4 elements in the queueand then the queue is full.
0 1 2 3
10 20 30 40
Front Rear
int qinsert( )
{
if(rear == size-1)
Printf(“Queue is Full”); else
{
rear = rear +1 ;
q[rear]= item;
}
}
-1 0 1 2 3
Rear Front
Rear/Front
Front Rear
int qdelete( )
{
if(front > rear )
printf (“Queue is Empty”);
else
front = front +1 ;
}
int qdisplay( )
{
if(front > rear ) printf(“Queue
is Empty”); else
{
printf(“The status of Queue \n”);
for(i= front ; i<=rear ;i++)
printf(“%d”, q[i]);
}
}
#include<stdio.h>
#define SIZE 5
int q[SIZE], rear= -1,front =0, item ,ch;
void qinsert(int );
void qdelete();
void display ();
void main ( )
{
clrscr ( );
while (1)
{
printf (“\n 1: INSERT2:DELETE 3:DISPLAY Otherwise EXIT\n”);
scanf (“%d”, &ch);
switch (ch)
{
case 1: printf (“Enter the element \n”);
scanf (“%d”, &item);
qinsert(item);
break;
case 2: qdelete ( ); break ;
case 3: qdisplay ( ); break default: printf
(“Invalid operation”);
getch ( );
exit (0);
}
}// end of while
} // end of main program
Circular Queue
A Circular Queue is a special version of queue where the last element of the
queue is connected to the first element of the queue forming a circle.
The operations are performed based on FIFO (First In First Out) principle. It is
also called ‘Ring Buffer’.
void cqdisplay( )
{
int i,X;
if(c==0)
printf("Queue is Emplty");
else
{
X=f;
printf("The status of queue\n");
for(i=1;i<=c;i++)
{
printf("%d ",q[X]);
X=(X+1)%MAX;
}
}
}
Priority Queue
A priority queue is a special type of queue. Each queue’s item has an additional piece of
information, namely priority. Unlike a regular queue, the values in the priority queue are
removed based on priority instead of the first-in-first-out (FIFO) rule.
Here, A,B, etc. denotes the value of items while 1,2 etc. denotes the priority of
items. So the item with the highest priority in this example is C (with the priority
of 1) that is removed first. And the lowest priority item, Q (with the priority of
19), will be removed at the end of the process.
void main()
{
int ch;
clrscr();
for(;;)
{
printf("1. insert\n 2. delete\n 3. display\n 4. exit\n");
printf("enter your ch\n");
scanf("%d",&ch);
switch(ch)
{
case 1:pqinsert(); break;
case 2: pqdel();break;
case 3: display(); break;
default: exit(0);break;
//getch();
}
}
}
Deque or Double Ended Queue is a type of queue in which insertion and removal of
elements can either be performed from the front or the rear. Thus, it does not follow FIFO
rule (First In First Out).
Operations on a Deque
Below is the circular array implementation of deque. In a circular array, if the array is full, we start
from the beginning.
But in a linear array implementation, if the array is full, no more elements can be inserted. In each of
the operations below, if the array is full, "overflow message" is thrown.
In this operation, the element is inserted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is full or not. If the
queue is not full, then the element can be inserted from the front end by using the below
conditions -
o If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
o Otherwise, check the position of the front if the front is less than 1 (front < 1), then
reinitialize it by front = n - 1, i.e., the last index of the array.
In this operation, the element is inserted from the rear end of the queue. Before
implementing the operation, we first have to check again whether the queue is full or not.
If the queue is not full, then the element can be inserted from the rear end by using the
below conditions -
o If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
o Otherwise, increment the rear by 1. If the rear is at last index (or size - 1), then
instead of increasing it by 1, we have to make it equal to 0.
In this operation, the element is deleted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or not.
If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform
the deletion. If the queue is not full, then the element can be inserted from the front end
by using the below conditions
If the deque has only one element, set rear = -1 and front = -1.
Else if front is at end (that means front = size - 1), set front = 0.
In this operation, the element is deleted from the rear end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or not.
If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform
the deletion.
If the deque has only one element, set rear = -1 and front = -1.
Applications of deque
1. Deque can be used as both stack and queue, as it supports both operations.
2. Deque can be used as a palindrome checker means that if we read the string from both
ends, the string would be the same.
Video Link:
https://www.youtube.com/watch?v=kLBuJ1Hle8g&t=87s
void f_insert()
{
if(f == 0)
printf("Space not available.\n");
else if(f > 0)
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[--f]);
}
Else
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[++f]);
++r;
}
}
void r_insert()
{
if(r == SIZE -1)
printf("Space not available.\n");
else
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[++r]);
}
if(f == -1)
f++;
}
void f_delete()
{
if(f == -1)
printf("Queue empty\n");
else if(f == r)
{
printf("The deleted element is: %d.\n",q[f]);
f = -1;
r = -1;
}
else
{
printf("The deleted element is: %d.\n",q[f]);
f++;
}
void r_delete()
{
if(r == -1)
printf("Queue is empty.\n");
void display()
{
if(r == -1)
printf("Queue is empty\n");
else
{
printf("Elements of the queue are: \n");
for(i = f; i <= r; i++)
printf("%d\t",q[i]);
}
}
void main(){
char ch;
system("CLS");
while(1){
printf("Double Ended Queue Operations: \n");
printf("1. Insert Elements to the front end.\n");
printf("2. Delete Elements from the front end.\n");
printf("3. Insert Elements to the rear end.\n");
printf("4. Delete Elements from the rear end.\n");
printf("5. Display the Elements of the Queue.\n");
printf("6. Exit.\n");
ch = getchar();
switch(ch)
{
case '1': f_insert();
break;
case '2': f_delete();
break;
case '3': r_insert();
break;
case '4': r_delete();
break;
case '5': display();
break;
case '6': exit(0);
break;
default: printf("Wrong input. Please try again.\n");
}
printf("\nPress any key to continue...\n");
getch();
system("CLS");
}
}