Implementation of Queue Using Linked List
Queue: It is a linear data structure. Collection with access only to the first element inserted.
- First In First Out (FIFO)
- Enqueue/Dequeue
- Remove/Pop
- front, rear
- Make empty
A queue can be implemented by both an array and a linked list.
Code here 👇
#include<stdio.h>
#include<stdlib.h>
struct node *front = NULL, *rear = NULL;
struct node{
int data;
struct node *next;
};
void enqueue(int a){
struct node *new = malloc(sizeof(struct node));
new->data = a;
new->next = NULL;
if (front==NULL && rear==NULL)
{
front=rear=new;
}
else{
rear->next = new;
rear = new;
}
}
void dequeue(){
struct node *temp;
if (front==NULL)
{
printf("Queue is Empty\n");
}
else{
printf("Dequed element is %d\n",front->data);
temp = front;
front = front->next;
if(front==NULL){
rear = NULL;
}
free(temp);
}
}
int main(){
dequeue();
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
dequeue();
dequeue();
dequeue();
return 0;
}
You can refer this video for understanding.
https://youtu.be/Mlv2fMvt9b4
https://youtu.be/1tDjNwntufU