C++ program to implement circular queue ADT using an array

/* Write a C++ program to implement circular queue ADT using an array */

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class cqueue
{
int q[5],front,rare;
public:
cqueue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if(front ==-1 && rare == -1)
{
q[++rare]=x;
front=rare;
return;
}
else if(front == (rare+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rare= (rare+1)%5;
q[rare]=x;
}
 
void pop()
{
if(front==-1 && rare==	-1)
{
cout <<"under flow";
return;
}
else if( front== rare  )
{
front=rare=-1;
return;
}
front= (front+1)%5;
}
 
 
 
 
void display()
{
int i;
if( front <= rare)
{
for(i=front; i<=rare;i++)
cout << q[i]<<" ";
}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rare;i++)
{
cout << q[i]<< " ";
}
}
}
};
 
main()
{
 
int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT   2.DELETE   3.DISPLAY    4.EXIT\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.push(ch); break;
 
case 2: q1.pop(); break;
case 3: q1.display(); break;
case 4: exit(0);
}
}
}

OUTPUT

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element4

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element5

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice1
enter element3

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
4 5 3

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice2

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice3
5 3

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter ur choice4

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

4 thoughts on “C++ program to implement circular queue ADT using an array

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