C++ PROGRAM FOR IMPLEMENTATION OF QUEUE USING POINTERS



#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue
{
struct node *front,*rear;
public:
queue()
{
front=rear=NULL;
}
void insert();
void del();
void show();
};
void queue::insert()
{
int value;
struct node *ptr;
cout<<"\t insertion\n";
cout<<"enter a no.to insert:";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(front==NULL)
front=ptr;
else
rear->next=ptr;
rear=ptr;
cout<<"\n item inserted\n\n";
}
void queue::del()
{
if(front==NULL)
cout<<"queue empty\n";
else
{struct node*temp;
temp=front;
front=front->next;
cout<<"deleted value is "<<temp->data<<"\n";
delete temp;
}
}
void queue::show()
{
struct node *ptr1=front;
if(front==NULL)
cout<<"queue is empty"<<endl;
while(ptr1!=NULL)
{
cout<<ptr1->data<<"->";
ptr1=ptr1->next;
}
cout<<"END"<<endl;
}
void main()
{
int a=0;
queue q;
clrscr();
cout<<"**********QUEUE USING POINTER"<<endl;
cout<<"MENU"<<endl<<"1.INSERT"<<endl<<"2.DELETE"<<endl<<"3.SHOW"<<endl<<"4.EXIT"<<endl;
while(a<=3)
{
cout<<"enter the choice";
cin>>a;
switch(a)
{
case 1:
q.insert();
break;
case 2:
q.del();
break;
case 3:
q.show();
break;
case 4:
cout<<"PROGRAM TERMINATED"<<endl;
}//switch
}//while
getch();
}

OUTPUT:
QUEUE USING POINTER
MENU
1.INSERT
2.DELETE
3.SHOW
4.EXIT
enter the choice1
         insertion
enter a no.to insert:10

 item inserted

enter the choice1
         insertion
enter a no.to insert:10

 item inserted

enter the choice3
10->10->END
enter the choice1
         insertion
enter a no.to insert:30

 item inserted

enter the choice2
deleted value is 10
enter the choice3
10->30->END
enter the choice

No comments:

Post a Comment