PROGRAM FOR QUEUE (INSERTION & DELETION)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
# define size 5
class queue
{
int front,rear,a[size];
public:
queue()
{
front=0;rear=0;
}
void addQ(int item)
{
if(rear==size)
cout<<"Queue is full"<<endl;
else
a[rear++]=item;
}
void delQ(int item)
{
if(front==rear)
{
cout<<"Queue is empty"<<endl;
}
else
{
a[front++];
}
}
void display()
{
int i;
if(front==rear)
{
cout<<"\n Queue is empty";
getch();
return;
}
for(i=front+1;i<=rear;i++)
cout<<a[i]<<"\t";
}
};
void main()
{
queue q1;
int item;
clrscr();
int choice;
do
{
clrscr();
cout<<endl<<"Main menu";
cout<<endl<<"1.Add\t 2.Delete\t 3.Display\t 4.Quit\t";
cout<<endl<<"Enter ur choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<endl<<"Enter an element to add:";
cin>>item;
q1.addQ(item);
break;
case 2:cout<<endl<<"Enter an element to delete:";
cin>>item;
q1.delQ(item);
break;
case 3:q1.display();
break;
case 4:exit(0);
}
getch();
}while(1);
}
#include<conio.h>
#include<stdio.h>
#include<process.h>
# define size 5
class queue
{
int front,rear,a[size];
public:
queue()
{
front=0;rear=0;
}
void addQ(int item)
{
if(rear==size)
cout<<"Queue is full"<<endl;
else
a[rear++]=item;
}
void delQ(int item)
{
if(front==rear)
{
cout<<"Queue is empty"<<endl;
}
else
{
a[front++];
}
}
void display()
{
int i;
if(front==rear)
{
cout<<"\n Queue is empty";
getch();
return;
}
for(i=front+1;i<=rear;i++)
cout<<a[i]<<"\t";
}
};
void main()
{
queue q1;
int item;
clrscr();
int choice;
do
{
clrscr();
cout<<endl<<"Main menu";
cout<<endl<<"1.Add\t 2.Delete\t 3.Display\t 4.Quit\t";
cout<<endl<<"Enter ur choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<endl<<"Enter an element to add:";
cin>>item;
q1.addQ(item);
break;
case 2:cout<<endl<<"Enter an element to delete:";
cin>>item;
q1.delQ(item);
break;
case 3:q1.display();
break;
case 4:exit(0);
}
getch();
}while(1);
}
Comments
Post a Comment