Implementing queue with arrays

//Implementing queue with arrays//

#include"iostream.h"
#include"conio.h"
#define NIL -1
#define MAX 10
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))
{
cout<<"OVERFLOW"; getch(); return; } if(t->front==NIL)
t->front=t->rear=0;
else if(t->rear==MAX-1)
{
for(i=t->front;i<=t->rear;i++)
t->terms[i-t->front]=t->terms[i];
t->rear=t->rear-t->front+1;
t->front=0;
}
else
t->rear++;
t->terms[t->rear]=x;
return;
}
void queue::display(void)
{
if((q->front==NIL)&&(q->rear==NIL))
{
cout<<"UNDERFLOW"; return; } for(int i=q->front;i<=q->rear;i++)
cout <<>terms[i];
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
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->Display \n";
cout<<"4->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<<"Queue" << endl; qt.display();
getch(); break; case '4': cout<<"Exiting";
getch(); break; default:
cout<<"Not a valid choice"; getch(); break; } }while(ch!=4);
getch();
return(0);
}

Post a Comment

Previous Post Next Post