#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
const int max=5;
class queue
{
private:
int qu[max];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void addq();
void delq();
void show();
};
void queue:: addq()
{
int item;
if(rear==max-1)
{
cout<<endl<<"Queue is Full";
return;
}
rear++;
cout<<endl<<"Enter the item to be inserted:";
cin>>item;
qu[rear]=item;
if(front==-1)
{
front=0;
}
}
void queue:: delq()
{
int item;
if(front ==-1)
{
cout<<endl<<"Queue is empty";
return;
}
item=qu[front];
cout<<"\nItem Deleted:"<<item;
if(front==rear)
{
front=rear-1;
}
else
{
front++;
return;
}
}
void queue:: show()
{
if(front ==-1)
cout<<endl<<"Queue is empty";
else
{
cout<<"\nQueue Element:";
for(int i=front ;i<=rear;i++)
cout<<setw(4)<<qu[i];
}
}
void main()
{
int choice;
queue Q;
clrscr();
do{
cout<<"\n------Queue Operations-----------\n";
cout<<"\n1.Insert";
cout<<"\n2.Delete";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1: Q.addq();
break;
case 2: Q.delq();
break;
case 3: Q.show();
break;
case 4: exit(0);
default: cout<<"\nWrong Choice!!";
}
}while(choice!=4);
getch();
}
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
const int max=5;
class queue
{
private:
int qu[max];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void addq();
void delq();
void show();
};
void queue:: addq()
{
int item;
if(rear==max-1)
{
cout<<endl<<"Queue is Full";
return;
}
rear++;
cout<<endl<<"Enter the item to be inserted:";
cin>>item;
qu[rear]=item;
if(front==-1)
{
front=0;
}
}
void queue:: delq()
{
int item;
if(front ==-1)
{
cout<<endl<<"Queue is empty";
return;
}
item=qu[front];
cout<<"\nItem Deleted:"<<item;
if(front==rear)
{
front=rear-1;
}
else
{
front++;
return;
}
}
void queue:: show()
{
if(front ==-1)
cout<<endl<<"Queue is empty";
else
{
cout<<"\nQueue Element:";
for(int i=front ;i<=rear;i++)
cout<<setw(4)<<qu[i];
}
}
void main()
{
int choice;
queue Q;
clrscr();
do{
cout<<"\n------Queue Operations-----------\n";
cout<<"\n1.Insert";
cout<<"\n2.Delete";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case 1: Q.addq();
break;
case 2: Q.delq();
break;
case 3: Q.show();
break;
case 4: exit(0);
default: cout<<"\nWrong Choice!!";
}
}while(choice!=4);
getch();
}
No comments:
Post a Comment