# Implementation of 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.

## Steps for implementing queue using linked list

### 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 implementing queue using linked list

### 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

### 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

```#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 print()//function to display the queue
{
struct node* temp;
if((front == NULL)&&(rear == NULL)){
printf("\nQueue is Empty");
}
else{
temp = front;
while(temp){
printf("\n%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
{
int opt,n,i,data;
while(opt!=0){
printf("\n\n1 for Insert the Data in Queue\n2 for show the Data in Queue \n3 for Delete the data from the Queue\n0 for Exit");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nEnter the size: ");
scanf("%d",&n);
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");

}
}
}```
```Output:

1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit
1

Enter the size: 3

12
24
36

1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit
3

1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit
2

24
36

1 for Insert the Data in Queue
2 for show the Data in Queue
3 for Delete the data from the Queue
0 for Exit
0```

#### Queue using Array

Here we have already learned implementation of Queues using Linked List in C , click the button below to learn the implementation of queue using array