Code:
#include<iostream.h>
#include<conio.h>
struct Node {
int a,b;
Node *link;
};
Node *front=NULL, *rear=NULL, *ptr=NULL;
Node* createNode(int i, int j)
{
ptr = new Node;
ptr->a=i;
ptr->b=j;
ptr->link=NULL;
return ptr;
}
void Insert(Node *newptr)
{
if(rear!=NULL)
rear->link=newptr;
rear=newptr;
if(front==NULL)
front=newptr;
}
void Delete()
{
if(front==NULL)
cout<<"Queue underflow!!";
else
{
ptr=front;
front=front->link;
delete ptr;
}
}
void displayQueue()
{
ptr=front;
cout<<"\nFront --> ";
while(ptr!=NULL)
{
cout<<"("<<ptr->a<<","<<ptr->b<<")";
ptr=ptr->link;
if(ptr!=NULL)
cout<<" --> ";
}
cout<<" <-- Rear \n";
}
int main()
{
char ch='y';
int a, b;
cout<<"Do you want to add a new node?[y/n]";
cin>>ch;
while(ch=='y')
{
cout<<"\nEnter a:";
cin>>a;
cout<<"\nEnter b:";
cin>>b;
Insert(createNode(a, b));
cout<<"\nDo you want to add another node?[y/n]";
cin>>ch;
}
cout<<"\nDisplaying Queue!\n";
displayQueue();
cout<<"\nDo you want to remove the front node?[y/n]";
cin>>ch;
while(ch=='y')
{
Delete();
cout<<"\nDo you want to remove another node from front?[y/n]";
cin>>ch;
}
cout<<"\nDisplaying Queue!\n";
displayQueue();
cout<<"\nPress any key...";
getch();
return 0;
}
Output:
Do you want to add a new node?[y/n]y
Enter a:<any num 1>
Enter b:<any num 2>
Do you want to add another node?[y/n]y
Enter a:<any num 3>
Enter b:<any num 4>
<..add as much nodes as you want..>
Do you want to add another node?[y/n]y
Enter a:<any num m>
Enter b:<any num n>
Displaying Queue!
Front --> (<num 1>, <num 2>) --> (<num 3>, <num 4>) --> <...other nodes you added....> --> (<num m>, <num n>) <-- Rear
Do you want remove the front node?[y/n]y
Do you want to remove another node from front?[y/n]n
<..remove as many nodes as you want..........>
Displaying Queue!
Front --> (<num 3>, <num 4>) --> <...nodes you added minus deleted....> --> (<num m>, <num n>) <-- Rear
Press any key...
**msg me if any you've any problem; esp. output..**