Program To circular Queue using C++

//circular Queue//

#include
#include
#define NIL -1
#define MAX 5
typedef struct
{
int terms[MAX];
int front,rear;
}que;
class queue
{
que *q;
public:
queue()
{
q->front=NIL;
q->rear=NIL;
}
void create(int);
int del(void);
void display(void);
};
void queue::create(int x)
{
int i;
que *t=q;
if(((t->front==0) && (t->rear==MAX-1))||(t->front==t->rear+1))
{
cout<<"OVERFLOW";
getch();
return;
}
if(t->front==NIL)
t->front=t->rear=0;
else if(t->rear==MAX-1)
t->rear=0;
else
t->rear++;
t->terms[t->rear]=x;
return;
}
int queue::del(void)
{
int n;
que *t=q;
if((q->front==NIL)&&(q->rear==NIL))
{
cout<<"UNDERFLOW";
return(-1);
}
n=t->terms[t->front];
if(t->front==t->rear)
t->front=t->rear=NIL;
else if(t->front==MAX-1)
t->front=0;
else
t->front++;
return(n);
}
int main()
{
int ch,n,x,i;
queue qt;
do
{
clrscr();
cout<<"1->Insert \n";
cout<<"2->Delete \n";
cout<<"3->Exit\n";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case '1':
cout<<"Enter the term to insert:";
cin>>x;
qt.create(x);
getch();
break;
case '2':
x=qt.del();
if(x!=-1)
cout << "Deleted term is " << x;
getch();
break;
case '3':
cout<<"Exiting";
getch();
break;
default:
cout<<"Not a valid choice";
getch();
break;
}
}while(ch!=3);
getch();
return(0);
}

Post a Comment

Previous Post Next Post