0% found this document useful (0 votes)
26 views

Circular Queue Implementation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Circular Queue Implementation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write a program to implement the operations on Circular Queues

Queue Algorithm:

Algorithm to insert an element in a circular queue

 Step 1: Check if the queue is full (Rear + 1 % Maxsize = Front)

 Step 2: If the queue is full, there will be an Overflow error

 Step 3: Check if the queue is empty, and set both Front and Rear to 0

 Step 4: If Rear = Maxsize - 1 & Front != 0 (rear pointer is at the end of the queue
and front is not at 0th index), then set Rear = 0

 Step 5: Otherwise, set Rear = (Rear + 1) % Maxsize

 Step 6: Insert the element into the queue (Queue[Rear] = x)

 Step 7: Exit
Algorithm to delete an element from the circular queue

 Step 1: Check if the queue is empty (Front = -1 & Rear = -1)

 Step 2: If the queue is empty, Underflow error

 Step 3: Set Element = Queue[Front]

 Step 4: If there is only one element in a queue, set both Front and Rear to -1 (IF
Front = Rear, set Front = Rear = -1)

 Step 5: And if Front = Maxsize -1 set Front = 0

 Step 6: Otherwise, set Front = Front + 1

 Step 7: Exit
Program:
/*Program To Implement The Operations Of Circular Queue*/
#include <stdio.h>
#define max 100
int insert(int ele);
void del();
int display();
int q[max],front=-1,rear=-1;
int insert(int ele)
{
if(front==-1&&rear==-1)
{
front=0;
rear=0;
q[rear]=ele;
}
else if((rear+1)%max==front)
{
printf("Queue is OverFlow\n");
}
else
{
rear=(rear+1)%max;
q[rear]=ele;
}
return rear;
}
void del()
{
if((front==-1)&&(rear==-1))
printf("Queue is underflow..\n");
else if(front==rear)
{
printf("The deleted element is %d\n", q[front]);
front=-1;
rear=-1;
}
else
{
printf("The deleted element is %d\n", q[front]);
front=(front+1)%max;
}
}
int display()
{
int i=front;
if(front==-1&&rear==-1)
{
printf("Queue is empty..\n");
}
else
{
printf("The Elements In Queue Are:\n");
while(i<=rear)
{
printf("%d\n", q[i]);
i=(i+1)%max;
}
}
}
int main()
{
int c,x,n;
printf("Welcome To Circular Queue:\n");
printf("\t1.Insert\n");
printf("\t2.Delete\n");
printf("\t3.Display\n");
printf("Enter Your Choice:");
scanf("%d", &c);
while(c<4)
{
switch(c)
{

case 1: printf("Enter the number of Elements To be inserted In Circular Queue:");


scanf("%d",&n);
printf("Enter %d Elements:\n",n);
for(int i=0;i<n;i++)
{
scanf("%d", &q[i]);
x=q[i];
insert(x);
}
break;
case 2: del();
break;
case 3: display();
break;

}
printf("Enter Your Choice:");
scanf("%d",&c);
}
printf("Entered Choice Is Invalid\n");
return 0;
}
Output:
Eg 1:
Welcome To Circular Queue:
1.Insert
2.Delete
3.Display
Enter Your Choice:1
Enter the number of Elements To be inserted In Circular Queue:5
Enter 5 Elements:
22
24
26
28
30
Enter Your Choice:2
The deleted element is 22
Enter Your Choice:2
The deleted element is 24
Enter Your Choice:3
The Elements In Queue Are:
26
28
30
Enter Your Choice:

Eg 2:

Welcome To Circular Queue:


1.Insert
2.Delete
3.Display
Enter Your Choice:1
Enter the number of Elements To be inserted In Circular Queue:3
Enter 3 Elements:
11
13
15
Enter Your Choice:2
The deleted element is 11
Enter Your Choice:2
The deleted element is 13
Enter Your Choice:2
The deleted element is 15
Enter Your Choice:2
Queue is underflow..
Enter Your Choice:3
Queue is empty..
Enter Your Choice:

You might also like