Queue Using LinkedList

Algorithm

INPUT: FRONT IS A POINTER WHICH IS USED TO POINT TO THE ELEMENT FROM WHERE DELETION OF ELEMENT TAKES PLACE AND THE ELEMENT TO BE ENQUEUE AND REAR IS A POINTER WHICH POINTS TO THE ELEMENT FROM WHERE INSERTION TAKES PLACE
OUTPUT: QUEUE AFTER OPERATION
DATA STRUCTURE: QUEUE USING LINKEDLIST

Algo Enqueue

STEP 1: START
STEP 2: IF(REAR=SIZE-1)THEN
STEP 3: PRINT("QUEUE IS FULL")
STEP 4: ELSE
STEP 5: IF(FRONT=-1)THEN
STEP 6: FRONT=0
STEP 7: ENDIF
STEP 8: REAR=REAR+1
STEP 9: Q=NEWNODE(NODE)
STEP 10: Q->DATA=ITEM
STEP 11: REAR->LINK=NEW
STEP 12: NEW->LINK=NULL
STEP 13: ENDIF
STEP 14: END

Algo Dequeue

STEP 1: START
STEP 2: PTR=HEADER->LINK
STEP 3: HEADER->LINK=PTR->LINK
STEP 4: FRONT=FRONT+1
STEP 5: FRONT=PTR->LINK
STEP 6: RETURN PTR
STEP 7: END

Code

#include <stdio.h>#include <stdlib.h>
int size;
int rear=-1,front,item,size=4;
int a[4],n;
struct node {
 int data;
 struct node *link;
};
void newnode();
struct node *ptr,*news,*header;
void display();void enqueue();void dequeue();
int main()
{ 
header=malloc(sizeof(struct node));
 header->link=news;
 ptr=header;
 char ch;
 do{
 printf("enter 1 for enqueue operation in Queue , 2 for dequeue operation in Queue:\n");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
 enqueue();
 printf("Queue after operation\n");
 display();
 break;
 case 2:
 dequeue();
 printf("Queue after operation\n");
 display();
 break;
 default:
 printf("Wrong choice\n");
 }
 printf("do you want to do again?");
 scanf("%c",&ch);
 }
 while((getchar())!='n');
 display();
 printf("\n\n");
 return 0;
}
void enqueue(){
 if(rear==size-1)
 {printf("Queue Overflow");}
 else {
 printf("Enter the item you want to insert?\n");
 scanf("%d",&item);
 // if(front==-1){
 // front=0;
 // }
 newnode();
 news->data=item;
 
 ptr->link=news;
 news->link=NULL;
 ptr=news;rear++;
 }
}
void dequeue()
{
 if(front==-1||ptr->data==NULL)
 {
 printf("Queue underflow");
 }
 else{
 ptr=header->link;
 header->link=ptr->link;
 item=ptr->data;
 front++; 
 }
}
void newnode()
{
 news=malloc(sizeof(struct node));
}
void display()
{
 if(rear==-1)
 {
 printf("Queue underflows");
 }struct node *p;
 p=header->link;
 while(p!=NULL)
 {
 printf("%d ",p->data);
 p=p->link;
 }
 printf("\n");
}

Output

user@computer$ : enter 1 for enqueue operation in Queue , 2 for dequeue operation in Queue:
2
Queue underflowQueue after operation
Queue underflows
do you want to do again?y
enter 1 for enqueue operation in Queue , 2 for dequeue operation in Queue:
1
Enter the item you want to insert?
7
Queue after operation
7
do you want to do again?y
enter 1 for enqueue operation in Queue , 2 for dequeue operation in Queue:
2
Queue after operation
do you want to do again?n

Leave a Comment

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