/*program to perform Queue Operations using linked list*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
class node
{
int data;
node *link;
friend class queue;
};
class queue
{
node *front,*rear;
public:
queue()
{
front=rear=NULL;
}
void insertion();
void deletion();
void print();
};
void queue::insertion()
{
int num;
node *n;
cout<<"\n enter a number:";
cin>>num;
n=new node;
n->data=num;
n->link=NULL;
if(front==NULL)
{
front=rear=n;
cout<<"number is inserted";
return;
}
rear->link=n;
rear=n;
cout<<"number is inserted into queue \n";
}
void queue::deletion()
{
node *d;
if(front==NULL)
{
cout<<"\n underflow \n";
return;
}
if(front==rear)
{
front=rear=NULL;
cout<<"\n number is deleted";
return;
}
d=front;
front=front->link;
cout<<d->data<<"is deleted \n";
delete d;
}
void queue::print()
{
node *a=front;
while(a!=NULL)
{
cout<<"\n"<<a->data;
a=a->link;
}
}
void main()
{
queue q;
int op;
clrscr();
while(1)
{
cout<<"\n QUEUE ELEMENTS ARE::\n";
q.print();
cout<<"\n 1.insertion \n 2.deletion \n 3.Exit \n";
cout<<"enter your option \n";
cin>>op;
switch(op)
{
case 1:q.insertion();
break;
case 2:q.deletion();
break;
case 3:exit(0);
}
getch();
}
}
Output :
QUEUE ELEMENTS ARE ::
1.insertion
2.deletion
3.exit
enter your option:
1
enter a number : 25
number is inserted
QUEUE ELEMENTS ARE :: 25
1.insertion
2.deletion
3.exit
enter your option
2
number is deleted
0 comments:
Post a Comment