C++ program for linked queue
Linked queue
The major problem with the queue implemented using array is, It will work for only fixed number of data values. That means, the amount of data must be specified in the beginning itself. Queue using array is not suitable when we don't know the size of data which we are going to use. A queue data structure can be implemented using linked list data structure. The queue which is implemented using linked list can work for unlimited number of values.
Program:
#include<iostream>
using namespace std;
struct node{
int info ;
node *next ;
}*t;
node *front=NULL;
node *rear=NULL;
void insert(int n)
{node *tmp=new node;
tmp->info=n;
tmp->next=NULL;
if(front== NULL)
front=rear=tmp;
else{
rear->next=tmp;
rear=tmp;}
}
void del()
{
node *tmp=new node;
tmp=front;
front=front->next;//front becomes the next node
delete tmp;
}
void display()
{ node *f=front;
while(f!=NULL)
{cout<<f->info<<"->";
f=f->next;}
cout<<"!!!";
}
main()
{int val,p,ch,val1;
char c;
node *t;
do{cout<<"Enter the info to create new node :";
cin>>val;
insert(val);
cout<<"Want to add more : ";
cin>>c;
}while(c=='y'||c=='Y');
cout<<"Enter your choice \n1.Insert \n2.Delete\nChoice:" ;
cin>>ch;
switch(ch)
{case 1:cout<<"Enter value you want to insert";
cin>>val1;
insert(val1);
break;
case 2:del();}
cout<<"Display ";
display();
}

Comments
Post a Comment