#include <iostream>
using namespace std;
struct queue
{
int data;
queue* next;
};
queue* front;
queue* rear;
void enqueue(int x)
{
queue* temp= new queue();
temp->data=x;
if(front==NULL)
{
front=temp;
rear=temp;
rear->next=NULL;
}
else
{
temp->next=front;
front=temp;
}
}
void dequeue()
{
if(rear==NULL||front->next==NULL)
{ cout<<"Queue is empty"<<endl;
front=NULL;
}
else
{
queue* temp1;
temp1=front;
while(temp1->next!=rear)
{
temp1=temp1->next;
}
rear=temp1;
rear->next=NULL;
}
}
void display()
{cout<<"The queue is:";
queue* temp2=front;
while (temp2!=NULL)
{
cout<<temp2->data<<"->";
temp2=temp2->next;
}
cout<<"NULL"<<endl;
}
int main()
{ int n,x;
front=NULL;
rear=NULL;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"1.To insert an element in the queue."<<endl;
cout<<"2.To delete an element from the queue."<<endl;
cin>>n;
switch(n)
{
case 1 : cout<<"Enter Data:";
cin>>x;
enqueue(x);
break;
case 2: dequeue();
break;
}
display();
cout<<"Do you want to continue operation?y/n"<<endl;
cin>>ch;
}
return 0;
}
No comments:
Post a Comment