#include<iostream>
using namespace std;
struct queue
{
int data;
struct queue *next;
};
queue *front=NULL;
queue *rear=NULL;
int count=0;
/**********Inserting Element in Queue******************/
void enqueue(int x)
{
queue *temp;
temp=new queue();
temp->data=x;
temp->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=temp;
count++;
}
else
{
rear->next=temp;
rear=temp;
count++;
}
}
/*********displaying elements in Queue********************/
void display(int count)
{
queue *temp;
temp=front;
for(int i=0;i<count;i++)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
/*******Removing element from Queue*********************/
void dequeue()
{
if(front==NULL)
{
cout<<"Queue is empty"<<endl;
}
else
{
queue *temp;
temp=front;
front=front->next;
delete temp;
count--;
cout<<" \n Item Dequeued Successfully"<<endl;
}
}
/****************Driver Function************************/
int main()
{
int choice;
int x;
do
{
cout<<"Enter element is Queue :"<<endl;
cin>>x;
enqueue(x);
cout<<"Do you want to enQueue again?"<<endl;
cin>>choice;
}while(choice==1);
cout<<"After Enqueue"<<endl;
display(count);
do
{
dequeue();
cout<<"Do you want to DeQueue again?"<<endl;
cin>>choice;
}while(choice==1);
cout<<"After dequeue"<<endl;
display(count);
return 0;
}
0 comments:
Post a Comment