Queue using Linked List
Archived from my old blog, mikirinkode.online (2021–2022). Posted as it was written.
Contents
Hi guys, if previously we have discussed Queue with Array, this time we will use Linked List to create a queue. For those who don't know what Queue is? A Queue is a data structure that contains a collection of data where this has characteristic namely FIFO (First In First Out) i.e the first data to enter will be the first to leave. For more details, we have discussed it previously here. Now we focus on the implementation.
- Queue Array
- Queue Linked List

The use of Linked List in Queue is the same as using Linked List in general. It's just that we focus more on the nature of the Queue, namely FIFO (First In First Out). So it is highly recommended for those of you who do not understand the Linked List to understand it first.
Dequeue Operation

The dequeue operation on the Queue that implements the linked list will delete the node that is at the beginning of the linked list, namely the front/head node. Then the front node will be replaced with the next node and the node will be shifted.
As in the illustration on the side, there is a dequeue process, namely by issuing a node of value 9. This issuance will disconnect the pointer on node 9 which points to the next node. Then, the node after the front node will become the new front node.
Enqueue Operation

The enqueue operation on Queue that implements a linked list will add a new node at the end of the linked list, so the new node will be tail/rear on the linked list. As in the illustration above, where there is a queue with a linked list and an enqueue is taking place, namely the addition of nodes with a value of 15. Due to the addition at the end, the added node must point to null, then the rear node with a value of 7 will be pointed to the new node, namely node 15.
Implementation in C++
Source Code
#include <iostream>
using namespace std;
// node using struct
struct queue{
string name;
int age;
queue *next;
};
queue *head, *tail, *newNode, *currentnode, *temporarynode;
// function prototype
void enqueue(string name, int age);
void dequeue();
int countQueue();
bool isEmpty();
void display();
void clear();
/*
* Main Function
*/
int main(){
enqueue("Kang Mikirin Kode", 23);
display();
enqueue("Ezio", 29);
enqueue("Hayabusa", 35);
enqueue("Sarah Kerrigan", 26);
enqueue("Yuki", 17);
enqueue("Arthas Menethil", 40);
display();
dequeue();
dequeue();
display();
clear();
display();
cout << "Number of queues: " << countQueue() << endl;
return 0;
}
/*
* Linked List Operation
*/
// enqueue function
void enqueue(string name, int age){
if(isEmpty()){
head = new queue();
head->name = name;
head->age = age;
head->next = NULL;
tail = head;
cout << "successfully added a new element." << endl;
} else{
newNode = new queue();
newNode->name = name;
newNode->age = age;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
cout << "successfully added a new element." << endl;
}
}
// dequeue function
void dequeue(){
if(isEmpty()){
cout << "Queue is empty" << endl;
} else{
temporarynode = head;
head = head->next;
temporarynode->next = NULL;
delete temporarynode;
cout << "successfully remove the first element." << endl;
}
}
// function to calculate amount data in queue
int countQueue(){
if(head == NULL){
return 0;
} else{
int counter = 0;
currentnode = head;
while(currentnode != NULL){
counter++;
currentnode = currentnode->next;
}
return counter;
}
}
// function to check the queue is empty or not
bool isEmpty(){
if(countQueue() == 0){
return true;
} else{
return false;
}
}
// function to print each element (node) in queue
void display(){
cout << "Queue:" << endl;
if(isEmpty()){
cout << "Queue is Empty" << endl;
} else{
cout << "Number of queues: " << countQueue() << endl;
currentnode = head;
int num = 0;
while(currentnode != NULL){
cout << num+1 << ". " << currentnode->name << " - " << currentnode->age << " tahun." << endl;
currentnode = currentnode->next;
num++;
}
}
cout << endl;
}
// function to delete all element (node) in queue
void clear(){
if(isEmpty()){
cout << "Queue is empty" << endl;
} else {
head->next = NULL;
head = NULL;
cout << "successfully remove all element in Queue" << endl;
currentnode = head;
while(currentnode != NULL){
temporarynode = currentnode;
currentnode = currentnode->next;
// menghapus node
temporarynode->next = NULL;
delete temporarynode;
}
head = NULL;
}
}Output
successfully added a new element.
Queue:
Number of queues: 1
1. Kang Mikirin Kode - 23 tahun.
successfully added a new element.
successfully added a new element.
successfully added a new element.
successfully added a new element.
successfully added a new element.
Queue:
Number of queues: 6
1. Kang Mikirin Kode - 23 tahun.
2. Ezio - 29 tahun.
3. Hayabusa - 35 tahun.
4. Sarah Kerrigan - 26 tahun.
5. Yuki - 17 tahun.
6. Arthas Menethil - 40 tahun.
successfully remove the first element.
successfully remove the first element.
Queue:
Number of queues: 4
1. Hayabusa - 35 tahun.
2. Sarah Kerrigan - 26 tahun.
3. Yuki - 17 tahun.
4. Arthas Menethil - 40 tahun.
successfully remove all element in Queue
Queue:
Queue is Empty
Number of queues: 0Conclusion
Queue using a Linked List is the same as using a Linked List in general. It's just more emphasis on the FIFO nature of the Queue. Alright guys, we have implemented Linked List in Queue. Thanks for reading.
Keep writing code and Have a nice day!