Pages

Monday 8 November 2021

Program to illustrate Queue using Linked list in c++

   /* Program on Queue using linked list*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include <process.h>
#include<mem.h>
struct node
{
int info;
struct node *link;
};
struct node *front,*rear,*tmp,*q;

class rkslqueue
{
public:
void insert(void);
void del(void);
void display(void);
};

void rkslqueue::insert()
{
int added_item;
struct node* newnode = new node;
tmp=newnode;
cout<<"Add the new value to queue : ";
cin>>added_item;
tmp->info=added_item;
tmp->link=NULL;
if(front==NULL)
 front=tmp;
else
 rear->link=tmp;
   rear=tmp;
}/*End of insert()*/

void rkslqueue::del()
{
if(front == NULL)
cout<<"Queue underflow\n";
else
{       tmp=front;
cout<<"Deleted item is:"<<tmp->info;
front=front->link;
delete(tmp);
}
}/*End of del()*/

void rkslqueue::display()
{
q=front;
if(front == NULL)
{
cout<<"\nQueue is empty\n";
}
else
{
cout<<"\nSingly linked List is: \n";
while(q!=NULL)
{
cout<<"||"<<q<<"----->"<<q->info;
q=q->link;
}
cout<<"|| "<<NULL ;
}
}/*End of display() */

void main()
{
int choice;
rkslqueue sl;
front=NULL;
rear=NULL;
clrscr();
while(1)
{       cout<<"\n MENU \n";
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : " ;
cin>>choice;
switch(choice)
{
case 1:
sl.insert();
break;
case 2:
sl.del();
break;
case 3:
sl.display();
break;
case 4:
exit(1);
default :
cout<<"Wrong choice\n";
}/*End of switch */
}/*End of while */
}/*End of main() */

Output:

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...