Bird
0
0
DSA Cprogramming~20 mins

Queue Implementation Using Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Enqueue and Dequeue Operations
What is the printed state of the queue after the following operations?

Enqueue 10, Enqueue 20, Dequeue, Enqueue 30
DSA C
struct Node {
    int data;
    struct Node* next;
};

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

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

int dequeue(struct Queue* q) {
    if (q->front == NULL) return -1;
    struct 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(struct Queue* q) {
    struct Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    struct Queue q = {NULL, NULL};
    enqueue(&q, 10);
    enqueue(&q, 20);
    dequeue(&q);
    enqueue(&q, 30);
    printQueue(&q);
    return 0;
}
A20 -> 30 -> null
B10 -> 20 -> 30 -> null
C30 -> null
D10 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that dequeue removes from the front of the queue.
Predict Output
intermediate
2:00remaining
Output after Multiple Dequeues
What is the output after these operations?

Enqueue 5, Enqueue 15, Enqueue 25, Dequeue, Dequeue, printQueue
DSA C
struct Node {
    int data;
    struct Node* next;
};

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

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

int dequeue(struct Queue* q) {
    if (q->front == NULL) return -1;
    struct 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(struct Queue* q) {
    struct Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    struct Queue q = {NULL, NULL};
    enqueue(&q, 5);
    enqueue(&q, 15);
    enqueue(&q, 25);
    dequeue(&q);
    dequeue(&q);
    printQueue(&q);
    return 0;
}
A25 -> null
B5 -> 15 -> 25 -> null
Cnull
D15 -> 25 -> null
Attempts:
2 left
💡 Hint
Two dequeues remove the first two elements.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dequeue Function
What error will occur when running this dequeue function if the queue is empty?
DSA C
int dequeue(struct Queue* q) {
    struct Node* temp = q->front;
    int val = temp->data;
    q->front = q->front->next;
    if (q->front == NULL) q->rear = NULL;
    free(temp);
    return val;
}
ACompilation error due to missing return type
BNo error, returns -1
CMemory leak error
DSegmentation fault due to dereferencing NULL pointer
Attempts:
2 left
💡 Hint
Check what happens if q->front is NULL before accessing temp->data.
Predict Output
advanced
2:00remaining
Output after Enqueue and Dequeue Sequence
What is the printed queue after these operations?

Enqueue 1, Enqueue 2, Enqueue 3, Dequeue, Enqueue 4, Dequeue, Enqueue 5, printQueue
DSA C
struct Node {
    int data;
    struct Node* next;
};

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

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

int dequeue(struct Queue* q) {
    if (q->front == NULL) return -1;
    struct 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(struct Queue* q) {
    struct Node* temp = q->front;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("null\n");
}

int main() {
    struct 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
B3 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> 4 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Each dequeue removes the front element.
🧠 Conceptual
expert
2:00remaining
Why Use Linked List for Queue Implementation?
Which of the following is the best reason to implement a queue using a linked list instead of an array?
ALinked list operations are always faster than arrays
BLinked list uses less memory than arrays always
CLinked list allows dynamic size without wasting memory or resizing
DLinked list does not require pointers
Attempts:
2 left
💡 Hint
Think about how arrays and linked lists handle size changes.