Queues using Linked List in C
How to implement Queue using linked list?
Implementation of Queues using Linked List in C solves the problem of Queue implementation with arrays as using linked list for implementing queue we need not to define the size of the queue and it can work on the infinite number of values.
Implementing queue using linked list will not change its behavior i.e. the queue will continue to work with FIFO architecture.
Queue Using Linked List in C
enqueue(data)
- Build a new node with given data.
- Check if the queue is empty or not.
- If queue is empty then assign new node to front and rear.
- Else make next of rear as new node and rear as new node.
dequeue()
- Check if queue is empty or not.
- If queue is empty then dequeue is not possible.
- Else store front in temp
- And make next of front as front.
print()
- Check if there is some data in the queue or not.
- If the queue is empty print “No data in the queue.”
- Else define a node pointer and initialize it with front.
- Print data of node pointer until the next of node pointer becomes NULL.
Algorithm for enqueue(int d)
- STRUCT NODE* NEW_N
- NEW_N->DATA = D
- NEW_N->NEXT = NULL
- IF((FRONT == NULL)&&(REAR == NULL))
- FRONT = REAR = NEW_N
- ELSE
- REAR->NEXT = NEW_N
- REAR = NEW_N
Algorithm for dequeue()
- STRUCT NODE *TEMP
- TEMP = FRONT
- IF((FRONT == NULL)&&(REAR == NULL))
- RETURN
- ELSE
- FRONT = FRONT->NEXT
- FREE(TEMP)
print()
- STRUCT NODE* TEMP
- IF((FRONT == NULL)&&(REAR == NULL))
- RETURN
- ELSE
- TEMP = FRONT
- WHILE(TEMP)
- RETURN TEMP->DATA
- TEMP = TEMP->NEXT
Code for implementing queue using linked list in C
Run
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; };//defining linked list to implement queue struct node *front = NULL; struct node *rear = NULL; void enqueue(int d)//function to insert a node in queue { struct node* new_n; new_n = (struct node*)malloc(sizeof(struct node)); new_n->data = d; new_n->next = NULL; if((front == NULL)&&(rear == NULL)){ front = rear = new_n; } else{ rear->next = new_n; rear = new_n; } } void display()//function to display the queue { struct node* temp; if((front == NULL)&&(rear == NULL)){ printf("\nQueue is Empty"); } else{ temp = front; while(temp){ printf(" %d ",temp->data); temp = temp->next; } } } void dequeue()//function to delete an element from a queue { struct node *temp; temp = front; if((front == NULL)&&(rear == NULL)){ printf("\nQueue is Empty"); } else{ front = front->next; free(temp); } } int main()//main function to use all our declared function { enqueue(5); enqueue(10); enqueue(15); enqueue(20); enqueue(25); printf("Queue:"); display(); printf("\nQueue After Dequeue:"); dequeue(); display(); }
Output
Queue: 5 10 15 20 25 Queue After Dequeue: 10 15 20 25
int main()//main function to use all our declared function
{
int opt=0,n,i,data;
while(opt!=4){
printf(“\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n4 for Exit\n\nEnter Your Choice:-“);
scanf(“%d”,&opt);
switch(opt){
case 1:
printf(“\nEnter the size: “);
scanf(“%d”,&n);
printf(“\nEnter your data\n”);
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 4:
break;
default:
printf("\nIncorrect Choice");
}
}
return 0;
}