Saturday, April 12, 2014

Linear queue using linked list

Program to implement linear queue using linked list

PROGRAM

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};

class lqueue
{
node *start,*last,*head;
public:
void insertion();
void deletion();
void display();
lqueue()
{
start=last=NULL;
}
};

void lqueue::insertion()
{
head=new node;
cout<<"Enter the data:";
cin>>head->data;
head->next=NULL;
if(start==NULL)
start=last=head;
else
{
last->next=head;
last=head;
}
}

void lqueue::deletion()
{
node *temp;
if(start==NULL)
cout<<"Linear queue is empty\n";
else
{
temp=start;
if(start==last)
start=last=NULL;
else
start=start->next;
cout<<"Deleted data is "<<temp->data;
delete temp;
}
}

void lqueue::display()
{
node *temp;
temp=start;
if(start==NULL)
cout<<"Linear queue is empty\n";
else
{
cout<<"The elements are:\n";
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
}

int main()
{
lqueue q1;
int ch;
char c;
do
{
cout<<"\nLinear Queue Operations";
cout<<"\n1.insertion\n”;
cout<<”2.deletion\n”;
cout<<”3.display\n”;
cout<<”Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:q1.insertion();
     break;
case 2:q1.deletion();
     break;
case 3:q1.display();
     break;
default:cout<<"Invalid option";
}
cout<<"\nDo you want to continue,y/n:";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}


OUTPUT

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:1
Enter the data:5

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:1
Enter the data:6

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:1
Enter the data:7

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:3
The elements are:
5               6              7

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:2
Deleted data is 5

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:2
Deleted data is 6

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:2
Deleted data is 7

Do you want to continue,y/n:y

Linear Queue Operations
1.insertion
2.deletion
3.display
Enter your choice:3
Linear queue is empty

Do you want to continue,y/n:n
 

0 comments:

Post a Comment