# How to implement Queue using Link List?

Hello Programmer,

How are you doing? In this tutorial I will show you how to implement Queue using Link list. Prerequisite : You have to know What is the link list ? How It works? And algorithm of Queue. Well, I assume you are familiar of those prerequisites. Implement Queue using Array

Well,Implement the Queue in link list, first you’ve to define a structure which has two part, one is data part another one is address part.(address of next node).

``````typedef struct Node{
int data;
struct Node *next;
}node;``````

Then you have to define two pointer variable .

``node *front,*rear;``

Then initialize a function where define front = rear = 0

``````void initial(){
front = rear = 0;
}``````

Well, Our initial setup has done, Now implement the enqueue function.

``````void enqueue(int val){
node *newNode;
newNode = new node;
newNode->data = val;
newNode->next = 0;
if(front == 0 && rear == 0){
front = rear =  newNode;
}else{
rear->next = newNode;
rear = rear->next;
}
}``````

dequeue function :

``````int dequeue(){
if(front == 0 && rear == 0){
cout<<"\nEmpty Queue!\n";
return -9999;
}
int data = front->data;
node *temp = front;
front = front->next;
free(temp);
return data;
}``````

display the values using display function :

``````void display(node *top){
node *temp = top;
if(top == 0){
cout<<"\nEmpty Queue!\n";
return;
}
while(temp != 0){
cout<<temp->data<<" ";
temp = temp->next;
}
}``````