C program to perform linked list implementation of Queue ADT

0
524

C program to perform linked list implementation of Queue ADT

CONSTRUCTION:

  1. Define the queue structure, read the size of the queue and choice of operation to be performed.
  2. Enqueue:
  • Read the element, ‘x’ to be inserted.
  • Check whether q.rear = q.size-1. If so print “queue full”.
  • Else, increment the ‘rear’ & place the element, ‘x’ at the rear position.
  1. Dequeue:
  • Initially front =0 and rear = -1. So check whether q.front > q.rear. If so print “queue empty”.
  • Delete the element in ‘front’ position.
  • Increment the ‘front’ pointer.
  1. Display:
  • If the queue is empty then print the message “queue is empty”
  • Else, display all elements in the queue.
  1. Isempty:
  • If q.front > q.rear then print “queue empty”.
  • Else, print “queue is not empty”
  1. Isfull :
  • If q.rear = q.size-1 then print “queue full.
  • Else print “queue is not full”

PROGRAM:

#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *front=NULL,*rear=NULL;
int n,item,quit=0;
clrscr();
do
{
printf("OPERATION \n 1==INSERT \n 2==DELETE \n 3==DISPLAY \n 4==EXIT \n");
printf("\n enter the choice:");
scanf("%d", &n);
switch(n)
{
case 1:
printf("enter the element to be inserted");
scanf("%d", &item);
insertion(&front,&rear,item);
break;
case 2:
deletion(&front);
break;
case 3:
display(front);
break;
case 4:
quit=1;
exit(0);
break;
deafult:
printf("invalid operation.....");
}
}
while(!quit);
getch();
}
insertion(front,rear,i)
struct node **front,**rear;
int i;
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->next=NULL;
new->data=i;
if((*front)==NULL)
{
(*front)=new;
(*rear)=new;
}
else
{
(*rear)->next=new;
(*rear)=new;
}
return;
}
deletion(front)
struct node **front;
{
struct node *temp;
if((*front)!=NULL)
{
temp=*front;
(*front)=(*front)->next;
free(temp);
}
return;
}
display(front)
struct node *front;
{
printf("\n front->");
while(front!=NULL)
{
printf("%d->",(*front).data);
front=(*front).next;
}
printf("NULL\n");
return;
}

Leave a Reply