Skip to main content

How I Implemented a Queue Using a Linked List in C++

Breaking down FIFO, head/tail pointers, and building a dynamic queue from scratch.

Updated 3 min readMarkdown
Part of seriesDSA

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

  1. Node Class: Represents a single element in the queue. It stores the integer data and a pointer next initialized to nullptr.
  2. enqueue(int val): Creates a new node. If the queue is empty (head == nullptr), both head and tail point to this new node. Otherwise, we attach the new node to tail->next and move tail forward. (Quick tip from debugging this: forgetting to update tail = newNode will cause every new insertion to overwrite the second node!)
  3. dequeue(): Checks if the queue isn't empty, then simply shifts head to head->next.
  4. front(): Prints head->data so we can peek at the first element without removing it.
  5. display(): Uses a temporary pointer temp to walk from head to nullptr, 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

1 view

About the author

No comments yet. Be the first to comment.