A program to implement Linear Linked queues. The program has four options as given below and it continues till the user wants
a) Insert
b) Delete
c) Display
d) Exit

 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>

struct node
{
int data;
node *link;
};

int size,count=0;

node* r=NULL;
node* f=NULL;

void insert()
{
if(count<size)
{
if(r==NULL)
{
r=new node;
cout<<"\nEnter the data: ";
cin>>r->data;
r->link=NULL;
f=r;
count++;
}
else
{
node *x;
x=new node;
cout<<"\nEnter the data: ";
cin>>x->data;
r->link=x;
x->link=NULL;
r=x;
count++;
}
}
else
cout<<"\nQueue Overflow.\n";
}

void del()
{
node *x;
if(count>0)
{
x=f;
f=f->link;
x->link=NULL;
cout<<"\nValue deleted: "<<x->data<<endl;
delete x;
count--;
}
else
cout<<"\nQueue Underflow.\n";
}

void display()
{
node *x;
x=f;
cout<<"\nQueue: ";
if(count>0)
{
while(x!=NULL)
{
cout<<x->data<<" ";
x=x->link;
}
cout<<endl;
}
else
cout<<"\nQueue empty.\n";
}

void main()
{
 clrscr();
 int ch;
 cout<<"Enter the size of the Link List: ";
 cin>>size;
 cout<<endl;

 do
  {
   cout<<"1.Insert"<<endl;
   cout<<"2.Delete"<<endl;
   cout<<"3.Display"<<endl;
   cout<<"4.Exit"<<endl;
   cout<<"Enter the choice: ";
   cin>>ch;

   switch(ch)
    {
     case 1: {
                 insert();
                 break;
                }
     case 2: {
                 del();
                 break;
                }
     case 3: {
                 display();
                 break;
                }
     case 4: {
                 break;
                }
     default:{
                 cout<<"\nWrong choice entered."<<endl;
                 break;
                }
    }
   cout<<endl;
  }
 while(ch!=4);
}

 

< Back