Circular Queue in C++

Circular Queue | Circular Queue C++ Program | Circular Queue Algorithms


In a simple queue, a Rear that is used for insertion, it reaches N-1 that is the maximum size of queue, even though elements from the front are deleted and occupied space is free, it is not possible to add new elements. For example, now if we want to insert 50. we cannot insert it because our N is equal to 5 and rear is at index 4 So this space cannot be utilized. so in order to overcome this limitation we have a concept of the circular queue.

circular queue
A circular queue can be formed from the linear queue by joining both the rear and front ends of the queue. Circular Queue is not a simple linear but it's circular and its structure can be like the following figure:

circular queue
In the circular queue, initialize front=0 and rear 0, N=Maximum queue size, if Rear=N and front not equal to 1, The rear is made equal to 1. In simple words, the "First" element of the Queue becomes "Rear" most element, if and only if the "front" has moved forward.

circular queue
Consider an example:
Circular Queue With N=5

circular queue example
circular queue example


Circular Queue Insertion algorithm:


CQueue is a circular queue to store data-items. Rear represents insertion at location and Front represents the location from which the data element is to be removed N=Maximum size of CQueue. Initially Rear0 and Front = 0. 1. if Front 0 and Rear = 0 then set Front =1 and go to step 4 2. If Front =1 and Rear = N or Front=Rear+1 then Print "Circular Queue Overflow" and Return. 3. If Rear N then Set Rear=1 and go to step 5. 4. Else Rear Rear + 1 5. Set CQueue[Rear]= element

Circular queue deletion algorithm:

CQueue is a circular queue to store data-items. Rear represents insertion at location and Front represents the location from which the data element is to be removed N=Maximum size of CQueue. Initially Rear= 0 and Front = 0. 1. If Front = 0 then Print "Circular Queue Underflow" and return 2. Set element= CQueue[Front] 3. If Front = N then Set Front = 1 and Return. 4. If Front = Rear then Set Front = 1 and Rear = 1 and Return. 5. Set Front = Front + 1


Circular Queue in C++


#include<stdio.h>
#define MAX 3
int cqueue[MAX];
int front=-1;
int rear=-1;
void display()
{ int i;
if(front==-1)
{
printf("Queue is empty.\n");
return;}
else 
{
printf("Queue : ");
for(i=front;i!=rear;i=(i+1)%MAX)
printf("%d ",cqueue[i]);
printf("%d\n", cqueue[i]);
}}
void insert()
{
int data;
if(front==(rear+1)%MAX)
{
printf("Queue overflow.\n");
return; }
else 
{
printf("Enter the element to be inserted : ");
scanf("%de", &data);
if(front==-1) front=0;
rear=(rear+1)%MAX;
cqueue[rear]=data;
display();
}}
void del()
{
int elem;
if(front==-1)
{
printf("Queue underflow\n");
return;
}
else 
{
elem=cqueue[front];
if(front==rear) front=rear=-1;
else
front=(front+1)%MAX;
printf("Element deleted is %d\n", elem);
display();
}}
int main()
{
int ch;
    do{
      printf("\nEnter your choice.\n");
  printf("1:Insert\n2:Delete\n3:Display\n4:Exit\n");
       scanf("%d", &ch);
       switch(ch)
       {
           case 1: insert(); break;
           case 2: del(); break;
  case 3:display(); break;
  case 4: break;
           default: printf("Invalid choice.\n");
       }
    }while(ch!=4);
    return 0;
}

OUTPUT:

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
1
Enter the element to be inserted: 1
Queue: 1
Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
1
Enter the element to be inserted: 2
Queue : 1 2

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
1
Enter the element to be inserted : 3
Queue : 1 2 3
Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
2
Element deleted is 1
Queue : 2 3
Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit

1
Enter the element to be inserted: 4
Queue : 2 3 4

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
4


Enter your choice.
1: Insert

2: Delete
3: Display
4: Exit
1

Enter the element to be inserted: 1
Queue: 1

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
1

Enter the element to be inserted: 2
Queue : 1 2

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
1

Enter the element to be inserted : 3
Queue : 1 2 3

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
2

Element deleted is 1
Queue : 2 3

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
Enter the element to be inserted: 4
Queue : 2 3 4

Enter your choice.
1: Insert
2: Delete
3: Display
4: Exit
4

You must be also searching for these programming languages :
Tags: Circular Queue, Circular queue C++, Circular queue in C++ program, Circular queue algorithm.

Post a Comment

Previous Post Next Post