#include<
#include<
#include<
int q[50],f,r,n;
class cqueue
{ public:
void enqueue();
void dequeue();
void front();
void rear();
void display();
void clearq();
void qsize();
};
void cqueue::enqueue()
{ int item;
if((f==0&&r==(n-1))||(f==r+1))
cout<<"\n overflow";
else if(r==(n-1))
{ r=0;
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<
cout<<"\n queue rear="<<
}
else if(f==-1&&r==-1)
{ f=r=0;
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<
cout<<"\n queue rear="<<
}
else
{
r=((r+1)%n);
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<
cout<<"\n queue rear="<<
}
}
void cqueue::dequeue()
{
if(f==-1&&r==-1)
{
cout<<"\n underflow";
}
else
{
cout<<"\n deleted value is "<<
q[f];
if(f==r)
f=r=-1;
else if(f==(n-1))
f=0;
else
f=((f+1)%n);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
}
void cqueue::front()
{
if(f>r||(f==-1&&r==-1))
cout<<"\n underflow";
else
cout<<"\n queue front="<<q[f];
cout<<"\n queue rear="<<r;
}
void cqueue::rear()
{
if(r>=n)
cout<<"\n overflow";
else
cout<<"\n queue rear="<<q[r];
cout<<"\n queue front="<<f;
}
void cqueue::display()
{
int i;
if(f==-1&&r==-1)
cout<<"\n no element element has been inserted";
else
{ if(f>r)
{ for(i=0;i<=r;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
for(i=f;i<n;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
else
{
if(r>=f)
{ for(i=f;i<=r;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue rear="<<r> ; }
}
} }
void cqueue::clearq()
{
if(f==-1)
cout<<"\\n no element inserted";
else
{
q[r]='\0';
}
f=-1;
r=-1;
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
void cqueue::qsize()
{if(f==-1&&r==-1)
cout<<"\n no element in the queue";
else if(f<=r)
{cout<<"\n queue size is"<<(r-f+1);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ; }
else
{ cout<<"\n queue size is"<<(n-f+r+1);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
}
void main()
{ f=-1;
r=-1;
int w=1,ch;
cqueue c;
clrscr();
cout<<"\n ENTER THE MAXIMUM SIZE OF THE QUEUE";
cin>>n;
while(w==1)
{
clrscr();
cout<<"\n WELCOME TO THE MENU ";
cout<<"\n ====================================================";
cout<<"\n 1.To insert an element into the queue \n 2.To delete an element from the queue \n 3.To display front of the queue \n 4.To dispaly the rear of the queue \n 5.To display the elements in the queue \n 6.TO clear the queue \n 7.size of queue \n 8.exit";
cout<<"\n ===================================================";
cout<<"\n PLEASE ENTER ANY OF THE ABOVE CHOICE ";
cin>>ch;
switch(ch)
{
case 1:c.enqueue();
break;
case 2:c.dequeue();
break;
case 3:c.front();
break;
case 4:c.rear();
break;
case 5:c.display();
break;
case 6:c.clearq();
cout<<"\n the queue has been cleared";
break;
case 7:c.qsize();
break;
case 8:exit(0);
default: cout<<"\n invalid entry";
}
cout<<"\n press (1) to continue or any other digit to exit";
cin>>w;
}
}
No comments:
Post a Comment