AIM:
To implement queue in the list.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Declare the structure node.
Step 4: Display the choices as
1. Addition
2. Deletion
Step 5: Get the choice as input.
Step 6: If the choice is 1, then add the data into the queue.
Step 7: If the choice is 2, then pop the data entered first from the queue.
Step 8: Stop the program.
PROGRAM:
#include<iostream.h>
struct node
{
int data;
struct node*ptr;
};
typedef struct node queue;
queue *addnode(queue *);
queue *delnode(queue *);
queue *head=NULL;
queue *rear=NULL;
int k=1;
main()
{
int ch,k=1;
char d;
queue *q;
do
{
cout<<"\nQUEUE IMPLEMENTATION";
cout<<"\nThe Choices are:\n1.Addition\n2.Deletion\n";
cout<<"Enter a choice:";
cin>>ch;
switch(ch)
{
case 1:
if(k==1)
{
q=(queue*)malloc(sizeof(queue));
head=rear=q;
cout<<"Enter the data:";
cin>>q->data;
q->ptr=NULL;
k=0;
}
else if(k==0)
{
rear=addnode(rear);
}
break;
case 2:
if(k==1)
cout<<"Queue is empty";
else if(head->ptr==NULL)
{
k=1;
cout<<"The deleted data is "<<head->data;
head=NULL;
rear=NULL;
}
else
head=delnode(head);
break;
}
cout<<"\nDo you want to continue?:";
cin>>d;
}
while(d=='y'||d=='Y');
}
}
queue *addnode(queue *p)
{
queue *t;
t=new node;
cout<<"Enter the data:";
cin>>t->data;
p->ptr=t;
t->ptr=NULL;
return t;
}
queue *delnode(queue *b)
{
int c;
c=b->data;
cout<<"The deleted data is "<<c;
head=b->ptr;
return head;
}
0 comments:
Post a Comment