#include
#include
#include
class queue
{
struct node
{
int data;
node *link;
}*front,*rear;
public:
queue();
void enqueue(int);
void dequeue();
void display();
~queue();
};
queue::queue()
{
front=rear=NULL;
}
void queue::enqueue(int item)
{
node *temp;
temp=new node;
if(temp==NULL)
{
cout<<"\nQueue is Full.....cannot insert";
return;
}
temp->data=item;
temp->link=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
void queue::dequeue()
{
node *temp;
if(front==NULL)
{
cout<<"\nThe Linked Queue is empty";
return;
}
temp=front;
cout<<"\nThe element deleted from linked queue ";
cout<
front=front->link;
delete(temp);
}
void queue::display()
{
node *temp;
temp=front;
cout<<"\nThe linked queue is now\n";
while(temp!=NULL)
{
cout<
temp=temp->link;
}
}
queue::~queue()
{
node *temp;
if(front==NULL)
{
return;
}
while(front!=NULL)
{
temp=front;
front=front->link;
delete(temp);
}
}
void main()
{
int ch,item;
queue q1;
do
{
cout<<"\n------------Linked Queue Operations-------------\n";
cout<<"\n1_Enqueue";
cout<<"\n2_Dequeue";
cout<<"\n3_Display";
cout<<"\n4_Exit";
cout<<"\nEnter your choice<1-4>:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the item to be inserted:";
cin>>item;
q1.enqueue(item);
break;
case 2:
q1.dequeue();
break;
case 3:
q1.display();
break;
case 4:
exit(0);
break;
default:
cout<<"\nInvalid choice";
break;
}
}while(ch!=4);
getch();
}
No comments:
Post a Comment