Coding Lab - TechOnTechnology

Program in C++ to demonstrate Queue using Linked Lists.




#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
class Que
{
struct node *front,*rear;
public:
Que(){front=rear=NULL;}
void ins();
void del();
void show();
};
void Que::ins()
{
int value;
struct node *ptr;
cout<<"\nEnter a number 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<<"\nSucess!!!";
}
void Que::del()
{
if(front==NULL)
{
cout<<"\nQueue is empty!!";
return;
}
struct node *temp;
temp=front;
front=front->next;
cout<<"\nDeleted value is "<<temp->data;
}
void Que::show()
{
struct node *ptr1=front;
if(front==NULL)
{
cout<<"\nThe Queue is empty!!";
return;
}
cout<<"\nThe Queue is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL";
}
void main()
{
clrscr();
Que q;
int choice;
while(choice!=4)
{
cout<<"\n\nMENU\n";
cout<<"1:INSERTION\n2:DELETION\n3:VIEW\n4:QUIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:q.ins();break;
case 2:q.del();break;
case 3:q.show();break;
case 4:choice=4;break;
default:cout<<"\nPlease enter correct choice(1-4)!!";break;
}
}
}





Name

Email *

Message *