Queue Using Linkedlist Representation

/*
Queue using Linkedlist representation.
*/
#include<iostream.h>
#include<conio.h>
#include<PROCESS.h>

struct node
{
int info;
node *next;
};

class queue
{
private:
node *front,*rear;
public:
queue();
void insert(int);
int del();
};
queue::queue()
{
front=NULL;
rear=NULL;
}
void queue::insert(int key)
{
node *nnode;

nnode=new node;
if(nnode==NULL)
{
cout<<“\nOverflow”;
}
else
{
nnode->info=key;
nnode->next=NULL;
if(front==NULL)
{
front=nnode;
}
else
{
rear->next=nnode;
}
rear=nnode;
}
}
int queue::del()
{
int key=-0;
node *temp;
if(front==NULL && rear==NULL)
{
cout<<“\nUnderflow.”;
}
else
{
key=front->info;
temp=front;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
delete(temp);
}
return(key);
}
int main()
{
queue q;
char c;
int ch,n;
clrscr();

do
{
cout<<“\nEnter==>”;
cout<<“\n1.Insert.”;
cout<<“\n2.Delete.”;
cout<<“\n\nEnter your choice: “;
cin>>ch;

switch(ch)
{
case 1: cout<<“\nEnter Information to be Inserted: “;
cin>>n;
q.insert(n);
break;
case 2: n=q.del();
cout<<“\nDeleted Information is “<<n;
break;
default:cout<<“\nInvalid Selection.”;
};
cout<<“\nDou you want to continue(Y/N): “;
cin>>c;
}while(c==’y’||c==’Y’);

getch();

return(0);
}
/*
OUTPUT:
——-
Enter==>
1.Insert.
2.Delete.

Enter your choice: 1

Enter Information to be Inserted: 2

Dou you want to continue(Y/N): y

Enter==>
1.Insert.
2.Delete.

Enter your choice: 2

Deleted Information is 2
Dou you want to continue(Y/N): n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.