Basic Queue Example

#include<iostream>
#include<conio.h>
#define MAX 5
using namespace std;

template <class t>
class queue
{
t data[5];
int rear,front;
public:
//Default constructor
queue()
{
rear=-1;
front=0;
}
//simple queue full
int full()
{

return(rear==4);
}
//simple queue empty
int empty()
{
  //  cout<<rear;
return(rear<front);
}
//simple queue add
void addqueue(t x)
{
if(!full())
{
rear ++;
data[rear]=x;
}
else
{
cout<<"full";
}
}
int removequeue()
{
int x=0;
if(!empty())
{
x=data[front];
front++;
}  
else
{
cout<<"empty";
}
return x;
}
int count()
{
return((rear-front)+1);
}
};
int main()
{
queue<int> q1;
int ch,cnt,x;

do{
    cout<<"enter \n1.add value\n2.remove value\n3.full\n4.empty\n";

cin>>ch;
if(ch==1)
{
     cout<<"Add Value ";
     cin>>x;      
q1.addqueue(x);

}
else if(ch==3)
{
   
if(q1.full())
{
 cout<<"full";     
 }
 else
 {
     cout<<"not full";
 }
}
else if(ch==2)
{
    
cout<<"remove "<<q1.removequeue()<<endl;

}
else if(ch==4)
{
if(q1.empty())
{
 cout<<"empty";           }else
{
    cout<<"not empty";
}
}

cout<<"do you want to continue then press 5\n";
cin>>cnt;
}

 while(cnt==5);
getch();
}
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-7462577112023113"
     data-ad-slot="1814817697"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Leave a Reply