Circular Queue – Program & Example – C++

A circular linked list is the link list in which the last node points to header node,
Manipulation of all the nodes are same as we do in the previous linked list.

Flash –

Program-

#include"conio.h"
#include"iostream"
using namespace std;
class list
{
	int *arr;
	int front,rear,max_size,c_size;
	public:
		list(int n)
		{
			max_size=n-1;
			c_size=0;
			arr=new int(max_size);
			for(int i=0;i<=max_size;i++) //will display the empty values as zero
				arr[i]=0;
			front=-1;
			rear=-1;
		}
		int ins(int);
		int delt();
		int disp();
};
int list::ins(int val)
{
	if(front==0 && rear == max_size || front==rear+1)
	{
		cout<<"\nOVERFLOW OCCURS";
		return 0;
	}
	else 
	{
		if(front == -1 && rear == -1)
		{
			front=0;
			rear=0;
			arr[front]=val;
		}
		else 
		{
			if(rear == max_size)
 			{
		 		rear=0;
		 		arr[rear]=val;
 			}
 			else
 			{
	 			rear++;
				arr[rear]=val;
 			}
		}
	}
 	c_size++;
}
int list::delt()
{
	int val;
	if(front==-1 && rear==-1)
	{
		cout<<"\nUNDERFLOW OCCURS";
		return 0;
	}
	else if(front == rear)
	{
		val=arr[front];
		arr[front]=0;
		front=-1;
		rear=-1;
	}
	else if(front == max_size)
	{
		val=arr[front];
		arr[front]=0;
		front=0;
	}
	else
	{
		val=arr[front];
		arr[front]=0;
		front++;
	}
	c_size--;
	cout<<"\nELEMENT POPPED : "<<val;
}
int list::disp()
{
	if(c_size==0)
	{
		cout<<"\nUNDERFLOW OCCURS";
		return 0;
	}
	cout<<"\n\nCircular Queue is : ";
	for(int i=0;i<=max_size;i++)
	{
		cout<<arr[i]<<" ";
	}
}
int main()
{
	int choice=1,x,s;
	cout<<"\nENTER SIZE OF QUEUE : ";
	cin>>s;
	list obj(s);
	while(choice!=0)
	{
		cout<<"\n\n1:INSERT 2:DELETE 3:DISPLAY 4:EXIT";
		cout<<" ::  ENTER CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\n\nENTER VALUE : ";
			        cin>>x;
			        obj.ins(x);
			        break;
   			case 2: obj.delt();
			        break;
      		case 3: obj.disp();
      			   break;
   			case 4: choice=0;
   				   break;
   			default: cout<<"\nMAKE CHOICE AGAIN";
		}
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
}

Output –

circ_queue

Leave a comment