How to write a c program to Create, Display, Insert and deletion of queue elements/Queue Implementation using doubly linked list in C Programming Language ?
Soution:
/* Create, Display, Insert and deletion of queue elements */
#include <stdio.h>
#include <stdlib.h>
struct queue* head, * tail;
void create(int);
void display ();
void insert (int);
void delete();
struct queue
{
int data;
struct queue *rptr,*lptr;
};
int main()
{
int i,n,b,value;
head=tail=NULL;
printf("\nEnter the option\n");
do
{
printf("\nEnter:\n1->Creation of Queue\n2->Display the queue\n3->Insert a member in the queue\n4->Delete a member of the queue\n0->Exit\n");
scanf("%d",&b);
switch(b)
{
case 1: printf("\nHow many nodes you want to enter\n");
scanf("%d", &n);
for(i=0; i< n; i++)
{
printf("\nEnter the value to be inserted in the queue\n");
scanf("%d",&value);
create(value);
}
break;
case 2: display();
break;
case 3: printf("\nEnter the value to be inserted in the queue\n");
scanf("%d",&value);
insert(value);
break;
case 4: delete();
break;
case 0: break;
}
if(b==0)
exit(0);
}
while(b!=0);
}
void create(int value)
{
struct queue* nn= malloc (sizeof (struct queue));
struct queue* temp= head;
nn->data=value;
nn->lptr=NULL;
nn->rptr=NULL;
if(head == NULL)
head=tail=nn;
else
{
while(temp->rptr != NULL)
temp = temp->rptr;
temp->rptr=nn;
nn->lptr= temp;
tail=nn;
}
}
void display()
{
int b;
struct queue* temp;
printf("\nPress:\n1->To print in forward direction\n2->To print in reverse direction\n");
scanf("%d", &b);
switch(b)
{
case 1: temp=head;
while(temp != NULL)
{
printf("\n%d\n",temp->data);
temp = temp->rptr;
}
break;
case 2: temp=tail;
while(temp != NULL)
{
printf("%d\n",temp->data);
temp=temp->lptr;
}
}
}
void insert(int value)
{
struct queue* nn=(struct queue*)malloc(sizeof(struct queue));
printf("\nInserting %d into the queue from rear end\n", value);
nn->data=value;
nn->rptr=NULL;
tail->rptr=nn;
nn->lptr=tail;
tail=nn;
}
void delete()
{
struct queue* temp;
temp=head;
head=head->rptr;
printf("\nDeleting %d from the queue from front end\n", temp->data);
free (temp);
head->lptr= NULL;
}