In this program we will take a look on working of queue by craeting, inserting, deleting and displaying an queue.
program:
#include<iostream>
#include<conio.h>
using namespace std;
class queue{
private:
int front,counter,maxsize,rear,q[5];
public:
queue();
void create();
void enqueue(int d);
void dequeue();
void show();
};
queue::queue()
{
front=-1;
rear=-1;
maxsize=5;
counter=0;
}
/*
void queue::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>q[i];
}
front=0;
rear=5;
}
*/
void queue::enqueue(int d)
{
if(rear==4)
{
cout<<"\n\nQueue is full\n";
}
else
{
cout<<"Enter data : ";
cin>>d;
rear++;
q[rear]=d;
if(front==-1)
{
front=rear=0;
}
}
}
void queue::dequeue()
{
if(front==rear||front==-1)
{
cout<<"\n\nQueue is empty\n";
}
else
{
front++;
cout<<"\n\nData is deleted\n";
if(front==4&&rear==4)
{
front=rear=-1;
}
}
}
void queue::show()
{
if(rear==-1&&front==-1)
{
cout<<"\nQueue is empty\n";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<"Data at ["<<i<<"] == "<<q[i]<<endl;
}
}
}
int main()
{
queue q;
int c,d;
// q.create();
do{
cout<<"\nPress 1 for enqueue\nPress 2 for dequeue\nPress 3 for show\nPress 4 for exit\nValue == ";
cin>>c;
switch(c)
{
case 1:
q.enqueue(d);
break;
case 2:
q.dequeue();
break;
case 3:
q.show();
break;
case 4:
return 1;
}
}
while(1);
}
#include<conio.h>
using namespace std;
class queue{
private:
int front,counter,maxsize,rear,q[5];
public:
queue();
void create();
void enqueue(int d);
void dequeue();
void show();
};
queue::queue()
{
front=-1;
rear=-1;
maxsize=5;
counter=0;
}
/*
void queue::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data at ["<<i<<"] : ";
cin>>q[i];
}
front=0;
rear=5;
}
*/
void queue::enqueue(int d)
{
if(rear==4)
{
cout<<"\n\nQueue is full\n";
}
else
{
cout<<"Enter data : ";
cin>>d;
rear++;
q[rear]=d;
if(front==-1)
{
front=rear=0;
}
}
}
void queue::dequeue()
{
if(front==rear||front==-1)
{
cout<<"\n\nQueue is empty\n";
}
else
{
front++;
cout<<"\n\nData is deleted\n";
if(front==4&&rear==4)
{
front=rear=-1;
}
}
}
void queue::show()
{
if(rear==-1&&front==-1)
{
cout<<"\nQueue is empty\n";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<"Data at ["<<i<<"] == "<<q[i]<<endl;
}
}
}
int main()
{
queue q;
int c,d;
// q.create();
do{
cout<<"\nPress 1 for enqueue\nPress 2 for dequeue\nPress 3 for show\nPress 4 for exit\nValue == ";
cin>>c;
switch(c)
{
case 1:
q.enqueue(d);
break;
case 2:
q.dequeue();
break;
case 3:
q.show();
break;
case 4:
return 1;
}
}
while(1);
}