7) Queue Linklist
7) Queue Linklist
7
Practical Name:-Implemetation of Queue using Linklist
Name:- ankush bhakare
Roll No:- 14
#include<iostream.h>
#include<conio.h>
class NODE
{
public:
int data;
NODE*link;
};
class QUEUE
{
NODE *front,*rear;
public:
QUEUE();
void ADD_151(int);
int DELETE_151();
void VIEWALL_151();
};
QUEUE::QUEUE()
{
front=NULL;
rear=NULL;
}
void QUEUE::ADD_151(int ele)
{
//step1 create a node
NODE *NN=new NODE();
//step 2 fill
NN->data=ele;
NN->link=NULL;
//step3
if(front==NULL)
{
front=NN;
rear=NN;
}
else
{
rear->link=NN;
rear=NN;
}
}
int QUEUE::DELETE_151()
{
if(front==NULL)
{
cout<<endl<<"QUEUE is empty";
return NULL;
}
else
{
int ele=front->data;
NODE *temp=front;
if(front==rear)
{
front=rear=NULL;
}
else
{
front=front->link;
}
delete temp;
return ele;
}
}
void QUEUE::VIEWALL_151()
{
if(front==NULL)
cout<<"Queue is empty";
else
{
NODE *ptr;
ptr=front;
while(ptr !=NULL)
{
cout<<" "<<ptr->data;
ptr=ptr->link;
}
}
}
void MENU()
{
QUEUE obj;
int ch,ele;
do
{
cout<<endl<<"1.ADD"<<endl;
cout<<"2.DELETE"<<endl;
cout<<"3.VIEWALL"<<endl;
cout<<"4.EXIT"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter elements"<<endl;
cin>>ele;
obj.ADD_151(ele);
break;
case 2:
ele=obj.DELETE_151();
if(ele!=NULL)
cout<<"deleted element is: "<<ele<<endl;
break;
case 3:
cout<<"Queue Elements are: "<<endl;
obj.VIEWALL_151();
break;
case 4:
return;
default:
cout<<"Invalid choice!";
}
}while(1);
}
void main()
{
clrscr();
MENU();
getch();
}