Bird
Raised Fist0
DSA Cprogramming~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 linked list queue
What is the printed state of the queue after enqueuing 10, 20, and 30 in order?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

typedef struct Queue {
    Node* front;
    Node* rear;
} Queue;

#include <stdio.h>
#include <stdlib.h>

void enqueue(Queue* q, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    if (q->rear == NULL) {
        q->front = q->rear = newNode;
        return;
    }
    q->rear->next = newNode;
    q->rear = newNode;
}

void printQueue(Queue* q) {
    Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    Queue q = {NULL, NULL};
    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);
    printQueue(&q);
    return 0;
}
A20 -> 30 -> null
B30 -> 20 -> 10 -> null
C10 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that enqueue adds elements at the rear of the queue.
❓ Predict Output
intermediate
2:00remaining
Output after enqueue and one dequeue operation
What is the printed state of the queue after enqueuing 5, 15, 25 and then dequeuing one element?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

typedef struct Queue {
    Node* front;
    Node* rear;
} Queue;

#include <stdio.h>
#include <stdlib.h>

void enqueue(Queue* q, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    if (q->rear == NULL) {
        q->front = q->rear = newNode;
        return;
    }
    q->rear->next = newNode;
    q->rear = newNode;
}

int dequeue(Queue* q) {
    if (q->front == NULL) return -1;
    Node* temp = q->front;
    int val = temp->data;
    q->front = q->front->next;
    if (q->front == NULL) q->rear = NULL;
    free(temp);
    return val;
}

void printQueue(Queue* q) {
    Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    Queue q = {NULL, NULL};
    enqueue(&q, 5);
    enqueue(&q, 15);
    enqueue(&q, 25);
    dequeue(&q);
    printQueue(&q);
    return 0;
}
A5 -> 15 -> 25 -> null
B15 -> 25 -> null
C25 -> null
Dnull
Attempts:
2 left
💡 Hint
Dequeue removes from the front of the queue.
🔧 Debug
advanced
2:00remaining
Identify the bug in enqueue function
Which option shows the correct fix to avoid memory leak in this enqueue function?
DSA C
void enqueue(Queue* q, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    if (q->rear == NULL) {
        q->front = newNode;
        // Missing assignment to q->rear here
        return;
    }
    q->rear->next = newNode;
    q->rear = newNode;
}
AAdd 'q->rear = newNode;' inside the if block before return
BFree newNode before return in the if block
CChange 'q->front = newNode;' to 'q->front = q->rear;'
DRemove the if block entirely
Attempts:
2 left
💡 Hint
Both front and rear must point to the new node when queue is empty.
🧠 Conceptual
advanced
1:30remaining
Why use linked list for queue enqueue?
Why is a linked list preferred over an array for implementing enqueue in a queue?
ALinked list allows dynamic size and O(1) enqueue without shifting elements
BLinked list uses less memory than arrays always
CLinked list stores elements in sorted order automatically
DLinked list requires less code to implement than arrays
Attempts:
2 left
💡 Hint
Think about resizing and shifting in arrays.
❓ Predict Output
expert
3:00remaining
Output after multiple enqueue and dequeue operations
What is the printed state of the queue after these operations? enqueue 1 enqueue 2 enqueue 3 dequeue enqueue 4 dequeue enqueue 5 printQueue
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

typedef struct Queue {
    Node* front;
    Node* rear;
} Queue;

#include <stdio.h>
#include <stdlib.h>

void enqueue(Queue* q, int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    if (q->rear == NULL) {
        q->front = q->rear = newNode;
        return;
    }
    q->rear->next = newNode;
    q->rear = newNode;
}

int dequeue(Queue* q) {
    if (q->front == NULL) return -1;
    Node* temp = q->front;
    int val = temp->data;
    q->front = q->front->next;
    if (q->front == NULL) q->rear = NULL;
    free(temp);
    return val;
}

void printQueue(Queue* q) {
    Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    Queue q = {NULL, NULL};
    enqueue(&q, 1);
    enqueue(&q, 2);
    enqueue(&q, 3);
    dequeue(&q);
    enqueue(&q, 4);
    dequeue(&q);
    enqueue(&q, 5);
    printQueue(&q);
    return 0;
}
A4 -> 5 -> null
B2 -> 3 -> 4 -> 5 -> null
C3 -> 4 -> 5 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Track the queue after each operation carefully.

Practice

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

Solution

  1. Step 1: Understand enqueue operation

    Enqueue means adding a new element to the queue.
  2. Step 2: Identify position of insertion in linked list queue

    In a linked list queue, new elements are added at the rear (end) to maintain order.
  3. Final Answer:

    Adds an element at the end (rear) of the queue -> Option B
  4. Quick Check:

    Enqueue = Add at rear [OK]
Hint: Enqueue always adds at the rear in a queue [OK]
Common Mistakes:
  • Confusing enqueue with dequeue operation
  • Thinking enqueue adds at the front
  • Mixing queue with stack operations
2. Which of the following is the correct syntax to create a new node for enqueue in C linked list?
easy
A. newNode = malloc(sizeof(Node));
B. struct Node newNode = (struct Node*)malloc(sizeof(struct Node));
C. Node newNode = malloc(sizeof(Node));
D. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

Solution

  1. Step 1: Allocate memory for new node

    In C, use malloc with sizeof(struct Node) to allocate memory.
  2. Step 2: Correct pointer declaration and casting

    Declare pointer as struct Node* and cast malloc result to (struct Node*).
  3. Final Answer:

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); -> Option D
  4. Quick Check:

    Correct malloc syntax = struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); [OK]
Hint: Always cast malloc to (struct Node*) in C [OK]
Common Mistakes:
  • Missing struct keyword in declaration
  • Not casting malloc result
  • Using wrong variable types
3. What will be the state of the queue after executing the following code?
struct Node {
  int data;
  struct Node* next;
};

struct Node* front = NULL;
struct Node* rear = NULL;

// Enqueue 10
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 10;
newNode->next = NULL;
if (rear == NULL) {
  front = rear = newNode;
} else {
  rear->next = newNode;
  rear = newNode;
}

// Enqueue 20
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 20;
newNode->next = NULL;
rear->next = newNode;
rear = newNode;
medium
A. 10 -> NULL
B. 20 -> 10 -> NULL
C. 10 -> 20 -> NULL
D. 20 -> NULL

Solution

  1. Step 1: Enqueue 10 when queue is empty

    Both front and rear point to new node with data 10.
  2. Step 2: Enqueue 20 when queue has one element

    rear->next points to new node with data 20, rear moves to this node.
  3. Final Answer:

    10 -> 20 -> NULL -> Option C
  4. Quick Check:

    Order of enqueue = 10 then 20 [OK]
Hint: New nodes link at rear, order stays FIFO [OK]
Common Mistakes:
  • Reversing order of nodes
  • Not updating rear pointer
  • Forgetting to set next to NULL
4. Identify the error in the following enqueue function for a linked list queue:
void enqueue(int value) {
  struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
  newNode->data = value;
  newNode->next = NULL;
  if (front == NULL) {
    front = newNode;
  } else {
    rear->next = newNode;
    rear = newNode;
  }
}
medium
A. rear pointer is not updated when queue is empty
B. newNode->next should point to front
C. malloc size is incorrect
D. front pointer is updated incorrectly

Solution

  1. Step 1: Check rear pointer update when queue is empty

    When front is NULL, only front = newNode is executed, rear remains uninitialized.
  2. Step 2: No rear update in if branch

    The else block sets rear = newNode, but it is skipped when queue is empty.
  3. Final Answer:

    rear pointer is not updated when queue is empty -> Option A
  4. Quick Check:

    rear must be set when queue is empty [OK]
Hint: Always update rear when queue was empty [OK]
Common Mistakes:
  • Not setting rear when queue is empty
  • Incorrect next pointer assignment
  • Wrong malloc size or casting
5. Given a linked list queue with elements 5 -> 15 -> 25 -> NULL, what will be the queue after enqueueing 35 and then 45? Show the final linked list state.
hard
A. 5 -> 15 -> 25 -> 35 -> 45 -> NULL
B. 45 -> 35 -> 25 -> 15 -> 5 -> NULL
C. 5 -> 15 -> 25 -> NULL
D. 35 -> 45 -> 5 -> 15 -> 25 -> NULL

Solution

  1. Step 1: Understand enqueue adds at rear

    Enqueue adds new nodes at the end of the queue to maintain order.
  2. Step 2: Add 35 and then 45 at the rear

    After adding 35, queue becomes 5 -> 15 -> 25 -> 35 -> NULL. Then adding 45 results in 5 -> 15 -> 25 -> 35 -> 45 -> NULL.
  3. Final Answer:

    5 -> 15 -> 25 -> 35 -> 45 -> NULL -> Option A
  4. Quick Check:

    Enqueue order preserved at rear [OK]
Hint: Enqueue appends at rear, order stays same [OK]
Common Mistakes:
  • Adding new nodes at front instead of rear
  • Reversing the order of elements
  • Not updating rear pointer after enqueue
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