How I Implemented a Queue Using a Linked List in C++
Breaking down FIFO, head/tail pointers, and building a dynamic queue from scratch.
When I first started learning Data Structures and Algorithms, implementing a Queue with an array made sense—until I hit the fixed-size limit. If you want a queue that can grow dynamically without worrying about resizing an array, a Linked List is a much cleaner fit.
Today, I sat down and built a Queue from scratch in C++ using a singly linked list. Here is how it works and the exact code I wrote.
Why Use Two Pointers (head and tail)?
A Queue follows the FIFO (First In, First Out) principle—just like a line at a coffee shop. You add people at the back and serve them from the front.
If we only kept track of the head pointer, adding a new element to the end of the linked list would take O(n) time because we'd have to traverse the whole list every time. By maintaining both a head (front) and a tail (rear) pointer:
- Enqueue (Insert at rear): Takes
O(1)time. - Dequeue (Remove from front): Takes
O(1)time.
The C++ Implementation
Here is the complete code I wrote to implement enqueue, dequeue, front, and display:
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node * next;
Node(int data){
this->data = data;
next = nullptr;
}
};
class que{
public:
Node* head;
Node* tail;
que(){
head = tail = nullptr;
}
void enqueue(int val){
Node* newNode = new Node(val);
if(head == nullptr){
head = tail = newNode;
cout<<val<<" Inserted to head "<<endl;
}else{
tail->next = newNode;
tail = newNode;
cout<<val<<" Inserted"<<endl;
}
}
void dequeue(){
if(head != nullptr){
head = head->next;
}else{
return;
}
}
void front(){
if(head != nullptr){
cout<<head->data<<endl;
}else{
return;
}
}
void display(){
Node* temp = head;
while(temp != nullptr){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
};
int main()
{
que q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.display();
q.dequeue();
q.display();
q.front();
return 0;
}
How Each Function Works
NodeClass: Represents a single element in the queue. It stores the integerdataand a pointernextinitialized tonullptr.enqueue(int val): Creates a new node. If the queue is empty (head == nullptr), bothheadandtailpoint to this new node. Otherwise, we attach the new node totail->nextand movetailforward. (Quick tip from debugging this: forgetting to updatetail = newNodewill cause every new insertion to overwrite the second node!)dequeue(): Checks if the queue isn't empty, then simply shiftsheadtohead->next.front(): Printshead->dataso we can peek at the first element without removing it.display(): Uses a temporary pointertempto walk fromheadtonullptr, printing every value along the way.
Console Output
Running the main() function gives this output:
10 Inserted to head
20 Inserted
30 Inserted
40 Inserted
50 Inserted
10 20 30 40 50
20 30 40 50
20
About the author
- 💻 Web Dev in progress..
Comments
Join the discussionNo comments yet. Be the first to comment.