Bird
Raised Fist0
DSA C++programming~20 mins

Enqueue Using Linked List in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of enqueue operations on a linked list queue
What is the printed state of the queue after performing the enqueue operations in the given code?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear->next = newNode;
        rear = newNode;
    }
    void printQueue() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.printQueue();
    return 0;
}
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
C10 -> 20 -> null
D20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that enqueue adds elements at the rear of the queue.
❓ Predict Output
intermediate
2:00remaining
Result after enqueueing and printing an empty queue
What will be the output if we enqueue a single element 5 and then print the queue?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear->next = newNode;
        rear = newNode;
    }
    void printQueue() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(5);
    q.printQueue();
    return 0;
}
A5 -> null
Bnull
C5
Dnull -> 5
Attempts:
2 left
💡 Hint
Check how the printQueue function formats the output.
❓ Predict Output
advanced
2:00remaining
Output after multiple enqueue operations with a bug in enqueue
Consider the following enqueue function with a bug. What will be the output after enqueueing 1, 2, 3 and printing the queue?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear = newNode;  // Bug: missing rear->next assignment
    }
    void printQueue() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.printQueue();
    return 0;
}
A3 -> null
B1 -> 2 -> 3 -> null
C1 -> null
Dnull
Attempts:
2 left
💡 Hint
Check if the next pointer of the rear node is updated correctly.
❓ Predict Output
advanced
2:00remaining
State of queue after enqueueing and dequeuing
Given the following code that enqueues 4, 5, 6 and then dequeues one element, what is the printed queue?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

class Queue {
    Node* front;
    Node* rear;
public:
    Queue() : front(nullptr), rear(nullptr) {}
    void enqueue(int val) {
        Node* newNode = new Node(val);
        if (!rear) {
            front = rear = newNode;
            return;
        }
        rear->next = newNode;
        rear = newNode;
    }
    void dequeue() {
        if (!front) return;
        Node* temp = front;
        front = front->next;
        if (!front) rear = nullptr;
        delete temp;
    }
    void printQueue() {
        Node* temp = front;
        while (temp) {
            std::cout << temp->data << " -> ";
            temp = temp->next;
        }
        std::cout << "null" << std::endl;
    }
};

int main() {
    Queue q;
    q.enqueue(4);
    q.enqueue(5);
    q.enqueue(6);
    q.dequeue();
    q.printQueue();
    return 0;
}
A4 -> 5 -> 6 -> null
Bnull
C6 -> null
D5 -> 6 -> null
Attempts:
2 left
💡 Hint
Dequeue removes the front element.
🧠 Conceptual
expert
2:00remaining
Why is rear pointer important in enqueue operation of linked list queue?
Which of the following best explains why maintaining a rear pointer is important when enqueueing in a linked list based queue?
AIt helps in removing elements from the front efficiently.
BIt allows adding new elements at the end in O(1) time without traversing the entire list.
CIt stores the total number of elements in the queue.
DIt points to the middle element to balance the queue.
Attempts:
2 left
💡 Hint
Think about how to add elements quickly at the end of a linked list.

Practice

(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Adds an element at the end (rear) of the linked list
B. Removes an element from the front of the linked list
C. Deletes the entire linked list
D. Adds an element at the front (head) of the linked list

Solution

  1. Step 1: Understand enqueue operation in queue

    Enqueue means adding an element to the queue.
  2. Step 2: Identify linked list position for enqueue

    In a linked list queue, enqueue adds at the rear (end) to maintain FIFO order.
  3. Final Answer:

    Adds an element at the end (rear) of the linked list -> Option A
  4. Quick Check:

    Enqueue = Add at rear [OK]
Hint: Enqueue always adds at the rear in linked list queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Adding element at front instead of rear
  • Thinking enqueue removes elements
2. Which of the following is the correct syntax to create a new node and enqueue it in a linked list queue in C++?
easy
A. Node* temp = Node(data); rear->next = temp; rear = temp;
B. Node temp = new Node(data); rear->next = temp; rear = temp;
C. Node* temp = new Node(data); rear->next = temp; rear = temp;
D. Node* temp = new Node; rear->next = temp; rear = temp;

Solution

  1. Step 1: Check node creation syntax in C++

    Correct way to create a node dynamically is Node* temp = new Node(data);.
  2. Step 2: Verify linking and updating rear pointer

    Link new node by rear->next = temp; and update rear to new node rear = temp;.
  3. Final Answer:

    Node* temp = new Node(data); rear->next = temp; rear = temp; -> Option C
  4. Quick Check:

    Use new with pointer and update rear [OK]
Hint: Use 'new' keyword and pointer for node creation [OK]
Common Mistakes:
  • Not using pointer (*) for node
  • Missing 'new' keyword for dynamic allocation
  • Incorrect assignment without pointers
3. What will be the state of the queue after executing the following enqueue operations on an empty linked list queue?
enqueue(10); enqueue(20); enqueue(30);
medium
A. 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. null -> 10 -> 20 -> 30
D. 20 -> 30 -> 10 -> null

Solution

  1. Step 1: Enqueue 10 on empty queue

    Queue becomes 10 -> null.
  2. Step 2: Enqueue 20 and 30 sequentially

    Adding 20 at rear: 10 -> 20 -> null; then 30 at rear: 10 -> 20 -> 30 -> null.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option B
  4. Quick Check:

    FIFO order maintained: 10, 20, 30 [OK]
Hint: Enqueue adds at rear, order stays same [OK]
Common Mistakes:
  • Reversing order of elements
  • Confusing enqueue with push front
  • Ignoring null termination
4. Identify the error in the following enqueue function for a linked list queue:
void enqueue(int data) {
  Node* temp = new Node(data);
  if (rear == nullptr) {
    front = temp;
  } else {
    rear->next = temp;
  }
  // Missing update of rear pointer here
}
medium
A. rear pointer is not updated to the new node
B. front pointer should be updated to temp always
C. Node creation syntax is incorrect
D. next pointer of temp should be set to front

Solution

  1. Step 1: Check rear pointer update after enqueue

    rear pointer must point to the newly added node to keep track of queue end.
  2. Step 2: Identify missing update in code

    Code misses rear = temp; after linking new node.
  3. Final Answer:

    rear pointer is not updated to the new node -> Option A
  4. Quick Check:

    rear must update to new node [OK]
Hint: Always update rear to new node after enqueue [OK]
Common Mistakes:
  • Forgetting to update rear pointer
  • Updating front pointer incorrectly
  • Setting next pointer wrongly
5. Given a linked list queue with elements 5 -> 15 -> 25 -> null, what will be the queue state after enqueueing 35 and then dequeueing one element?
hard
A. 35 -> 5 -> 15 -> 25 -> null
B. 5 -> 15 -> 25 -> 35 -> null
C. 25 -> 35 -> null
D. 15 -> 25 -> 35 -> null

Solution

  1. Step 1: Enqueue 35 at rear

    Queue becomes 5 -> 15 -> 25 -> 35 -> null.
  2. Step 2: Dequeue removes front element 5

    After dequeue, front moves to 15, so queue is 15 -> 25 -> 35 -> null.
  3. Final Answer:

    15 -> 25 -> 35 -> null -> Option D
  4. Quick Check:

    Enqueue rear, dequeue front; update pointers accordingly [OK]
Hint: Enqueue rear, dequeue front; update pointers accordingly [OK]
Common Mistakes:
  • Removing wrong element on dequeue
  • Not updating front pointer after dequeue
  • Adding new element at front instead of rear
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard