Circular Queue
Circular Queue
Circular Queue
STRUCTUR
E
DR. SOUMI DUTTA, IEM
APPLICATIONS OF SIMPLE QUEUE
Typical uses of queues are in simulations and operating systems.
0 1 2 3 4
11 22 33 44
Front Rear
1 3
OTHER TYPES OF SIMPLE QUEUE
1. Circular queue
3. Priority queue
CIRCULAR QUEUE
In circular queues the elements Q[0],Q[1],Q[2] .... Q[n – 1] is represented in a circular
fashion with Q[0] following Q[n-1]. A circular queue is one in which the insertion of a
new element is done at the very first location of the queue if the last location at the
queue is full.
Q[0]
Q[4] 0 1 2 3 4
11 22 33 44 55
Q[3] Q[1]
Q[2]
CIRCULAR QUEUE INSERTION
Rear Front 0 1 2 3 4
Empty Queue
-1 -1
0 1 2 3 4
11 Item=11
Front Rear
0 0
0 1 2 3 4
11 22 Item=22
Front Rear
0 1
CIRCULAR QUEUE INSERTION
0 1 2 3 4
11 22 33 Item=33
Front Rear
0 2
0 1 2 3 4
11 22 33 44 Item=44
Front Rear
0 3
0 1 2 3 4
11 22 33 44 55 Item=55
Front Rear
Overflow
Overflow
0 4
CIRCULAR QUEUE INSERTION
0 1 2 3 4
33 44 55
Front Rear
2 4
0 1 2 3 4
66 33 44 55 Item=66
Rear Front
0 2
0 1 2 3 4
66 77 33 44 55 Item=77
Rear Front
Overflow
Overflow
1 2
OVERFLOW Rear Front
Q[0]
Q[4]
1. front == 0 and rear == size-1 55 11
2. front==rear+1
44 22
Q[3] Q[1]
Rear
33
Q[0] Q[2]
Q[4]
15
55 16
11
Case: 1
Front
14
44 12
22
Q[3] Q[1]
13
33
Q[2] Case: 2
QUEUE DELETION
0 1 2 3 4
11 22 33 44 55 Item=11
Front Rear
1 4
0 1 2 3 4
11 22 33 44 55 Item=22
Front Rear
2 4
0 1 2 3 4
11 22 33 44 55 Item=33
Front Rear
3 4
QUEUE DELETION
0 1 2 3 4
11 22 33 44 55 Item=44
Front Rear
4 4
0 1 2 3 4
11 22 33 44 55 Item=55
Rear Front
-1 -1
Underflow Condition :
1. Front==-1
CIRCULAR QUEUE
DELETION
0 1 2 3 4
66 77 33 44 55 Delete = 33
Rear Front
1
3
0 1 2 3 4
66 77 33 44 55 Delete = 44
Rear Front
1 4
0 1 2 3 4
66 77 33 44 55 Delete = 55
Front Rear
4 1
CIRCULAR QUEUE
DELETION
0 1 2 3 4
66 77 33 44 55 Delete = 66
Front Rear
1 1
0 1 2 3 4
66 77 33 44 55 Delete = 77
Front
Rear
-1
-1
Underflow
Underflow
IMPLEMENTATION
# include<stdio.h>
# include<stdlib.h>
#include<conio.h>
# define size 5
int ch;
int q[size];
int rear = -1;
int front = -1;
void Insert_queue();
void Delete_queue(); Function Prototype
void Display_queue();
IMPLEMENTATION
void Insert_queue()
{
if ((front == 0 && rear == size-1) || (front==rear+1))
{
printf("\n Overflow"); Queue Overflow
return;
}
if(front==-1)
{
front=0; First Data Insertion
rear=0;
}
else
{
if (rear == size-1)
rear = 0; Circular Insertion
else
rear ++; General Insertion
}
}
IMPLEMENTATION
void Delete_queue()
{
if (front==-1)
{ Queue Underflow
printf("\nUnderflow");
return ;
}
ch = q[front];
printf("\n Element deleted %d:", ch); Display data to be Deleted
if(front == rear)
{
front = -1; Last Data Deletion
rear = -1;
}
else
{
if ( front == size-1) Circular Deletion
front = 0;
else
front++ ; General Deletion
}
}
Rear Front
IMPLEMENTATION Q[4]
Q[0]
void Display_queue() 55 11
{
int i;
if (front==-1 )
44 22
{ Q[3] Q[1]
printf("\nUnderflow");
return ; 33
}
if ( rear >= front) Q[2]
{
for(i = front; i <= rear; i++)
printf(" %d \n", q[i]);
Rear
}
else
{ Q[0]
Q[4]
for(i = front; i < size; i++) 15 16
printf(" %d \n", q[i]);
for(i = 0; i <= rear; i++)
printf(" %d \n", q[i]); Front
14 12
} Q[3] Q[1]
} 13
Q[2]
void main() {
int choice;
while(1) {
printf("\nInsert->1 \nDelete->2 \nDisplay->3 \nQuit-.4:");
printf("\nInput the choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1 :
Insert_queue();
break;
case 2 :
Delete_queue();
break;
case 3 :
Display_queue();
break;
case 4 :
exit(0);
}
}
}
DEQUES
A deque is a homogeneous list in which elements can be added or inserted (called
push operation) and deleted or removed from both the ends (which is called pop
operation). ie; we can add a new element at the rear or front end and also we can
remove an element from both front and rear end. Hence it is called Double Ended
Queue.
The possible operation performed on deque is
1. Add an element at the rear end
2. Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
Deletion Deletion
Insertion Insertion
-
77 65 98 34 23
INPUT RESTRICTED DEQUE
An input restricted deque is a deque, which allows insertion at only rear end, but
allows deletion at both ends, rear and front end of the lists.
Three operations:
•insert_right()
•delete_left()
•delete_right()
•display_queue()
Deletion Deletion
Insertion
77 65 98 34 23
0 1 2 3 4
OUTPUT RESTRICTED DEQUE
An output-restricted deque is a deque, which allows deletion at only front end, but
allows insertion at both ends, rear and front ends, of the lists.
Three operations:
•insert_right()
•insert_left()
•delete_left()
•display_queue()
Insertion Deletion
Insertion
77 65 98 34 23
0 1 2 3 4