Name: Mohini Vilas Narkhede
Roll no: 80
Practical no:
Title: Implementation of program based on Queue using Linked list.
#include<iostream.h>
#include<conio.h>
class NODE
{
public:
int data;
NODE *link;
};
class QUEUE
{
NODE *front, *rear;
public:
QUEUE();
void ADD_80(int);
int DEL_80();
void VIEWALL_80();
};
QUEUE::QUEUE()
{
front=NULL;
rear=NULL;
}
void QUEUE::ADD_80(int ele)
{
//step 1: create a node
NODE *NN = new NODE();
//step 2: fill node
NN->data=ele;
NN->link=NULL;
//step 3: set links
if(front==NULL)
{
front = NN;
rear = NN;
}
else
{
rear->link = NN;
rear = NN;
}
}
1
int QUEUE::DEL_80()
{
if(front==NULL)
{
cout<<endl<<"Queue is Empty";
return NULL;
}
else
{
int ele=front->data;
NODE *temp=front;
if(front==rear) // Only one node in Queue
{
front=rear=NULL;
}
else
{
front=front->link;
}
//free the deleted node memeory using 'delete' operator
delete temp;
return ele;
}
}
void QUEUE::VIEWALL_80()
{
if(front==NULL)
cout<<endl<<"Queue is Empty";
else
{
NODE *ptr;
ptr=front;
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
ptr=ptr->link;
}
}
}
void MENU()
{
int option,ele;
QUEUE obj;
do
{
cout<<"\n Select any option";
2
cout<<"\n 1.ADD";
cout<<"\n 2.DEL";
cout<<"\n 3.VIEWALL";
cout<<"\n 4.EXIT";
cout<<"\n Enter the option";
cin>>option;
switch(option)
{
case 1:
cout<<"\n Enter the element to add: ";
cin>>ele;
obj.ADD_80(ele);
break;
case 2:
obj.DEL_80();
if(ele!=NULL)
cout<<ele<<" is deleted";
break;
case 3:
cout<<endl<<"The elements are: ";
obj.VIEWALL_80();
break;
case 4:
return;
default:
cout<<endl<<"Invalid Choice";
}
}while(1);
}
void main()
{
clrscr();
MENU();
getch();
}