#include<iostream>
using namespace std;
void addqueue();
void deletequeue();
void display();
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL;
typedef struct queue node;
main()
{
int choice;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop ";
cout<<"\n 3.Display";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : addqueue();
break;
case 2 : deletequeue();
break;
case 3 : display();
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<4);
}
void addqueue()
{
int item;
node *temp;
temp=new node;
cout<<"\n Enter the item to insert : ";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{ front=temp;
rear=temp;
}
else
{ rear->next=temp;
rear=temp;
}
}
void deletequeue()
{
node *temp;
if(front==NULL)
cout<<"\n No item in list ";
else if(front==rear)
{
cout<<"item deleted is "<<front->data;
front=NULL;
rear=NULL;
}
else
{
temp=front;
cout<<"\n item popped is "<<temp->data;
front=front->next;
delete temp;
}
}
void display()
{
node *temp;
if(front==NULL)
cout<<"\n No item to display ";
else
{ temp=front;
cout<<"\n\n";
while(temp!=rear)
{
cout<<" "<<temp->data;
temp=temp->next;
}
cout<<" "<<temp->data;
}
}
using namespace std;
void addqueue();
void deletequeue();
void display();
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL;
typedef struct queue node;
main()
{
int choice;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop ";
cout<<"\n 3.Display";
cout<<"\n Enter the choice :";
cin>>choice;
switch(choice)
{
case 1 : addqueue();
break;
case 2 : deletequeue();
break;
case 3 : display();
break;
default : cout<<"\n Wrong choice! ";
break;
}
}while(choice>0&&choice<4);
}
void addqueue()
{
int item;
node *temp;
temp=new node;
cout<<"\n Enter the item to insert : ";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{ front=temp;
rear=temp;
}
else
{ rear->next=temp;
rear=temp;
}
}
void deletequeue()
{
node *temp;
if(front==NULL)
cout<<"\n No item in list ";
else if(front==rear)
{
cout<<"item deleted is "<<front->data;
front=NULL;
rear=NULL;
}
else
{
temp=front;
cout<<"\n item popped is "<<temp->data;
front=front->next;
delete temp;
}
}
void display()
{
node *temp;
if(front==NULL)
cout<<"\n No item to display ";
else
{ temp=front;
cout<<"\n\n";
while(temp!=rear)
{
cout<<" "<<temp->data;
temp=temp->next;
}
cout<<" "<<temp->data;
}
}
No comments:
Post a Comment