Program to implement circular queues using arrays


// Program to implement circular queues using arrays

#include<iostream.h>
#include<conio.h>
#include<process.h>

#define max 20
#define null -1

class Cqueue
{
	private:
		int cq[max];
		int front,rear;

	public:
		Cqueue()
		{
			front=rear=-1;
		}
		void insert();
		void delet();
		void display();
};


void Cqueue :: insert()
{
	int y;
	int x;
	y=(rear+1)%max;
	if(y==front)
		cout<<"Circular Queue is full";
	else
	{
		cout<<"\nEnter element to insert : ";
		cin>>x;

		if(front==null)
		{
			front=0;
			rear=0;
		}
		else
			rear=y;

		cq[rear]=x;
		cout<<"\nElement inserted";
	}
}


void Cqueue :: delet()
{
	int x;

	if(front==null)
		cout<<"\nCircular queue empty";
	else
	{
		x=cq[front];
		if(front==rear)
			front=rear=-1;
		else
			front=(front+1)%max;
		cout<<"\nDeleted element is: "<<x;
	}
}


void Cqueue :: display()
{
       int i;

	if(front==-1)
		cout<<"\nCircular queue is empty";
	else
	{
		cout<<"\nElements of circular queue are : ";
		for(i=front;i!=rear;i=(i+1)%max)
			cout<<cq[i]<<"\t";
		cout<<cq[rear];
	}
}


void main()
{
	Cqueue icq;
	int c;
	clrscr();

	while(1)
	{
		cout<<"\n\n1.Insert\n2.Delete\n3.Display\n4.exit\n";
		cout<<"\nEnter your choice: ";
		cin>>c;

		switch(c)
		{
			case 1:
					icq.insert();
					break;
			case 2:
					icq.delet();
					break;
			case 3:
					icq.display();
					break;
			case 4:
					exit(0);
		}
	}
}

Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in