Bird
0
0
DSA Cprogramming~20 mins

Push Using Linked List Node in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after pushing nodes?
Consider a linked list initially empty. We push nodes with values 10, 20, then 30 using the push function below. What is the printed linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;
    push(&head, 10);
    push(&head, 20);
    push(&head, 30);
    printList(head);
    return 0;
}
A20 -> 30 -> 10 -> NULL
B10 -> 20 -> 30 -> NULL
C30 -> 20 -> 10 -> NULL
DNULL
Attempts:
2 left
💡 Hint
Remember, push adds a new node at the beginning of the list.
Predict Output
intermediate
2:00remaining
What is the output after pushing nodes with duplicate values?
Given the push function, what is the linked list after pushing 5, 10, 5 in that order?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;
    push(&head, 5);
    push(&head, 10);
    push(&head, 5);
    printList(head);
    return 0;
}
A5 -> 5 -> 10 -> NULL
B10 -> 5 -> 5 -> NULL
CNULL
D5 -> 10 -> 5 -> NULL
Attempts:
2 left
💡 Hint
Push adds nodes at the front, so last pushed is first.
Predict Output
advanced
2:00remaining
What is the output after pushing nodes and modifying head incorrectly?
What happens if we push nodes but forget to update the head pointer correctly? Consider this code snippet:
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node* head, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = head;
    head = new_node;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;
    push(head, 1);
    push(head, 2);
    push(head, 3);
    printList(head);
    return 0;
}
ANULL
B3 -> 2 -> 1 -> NULL
CSegmentation fault
D1 -> 2 -> 3 -> NULL
Attempts:
2 left
💡 Hint
Check how the head pointer is passed and updated.
Predict Output
advanced
2:00remaining
What is the output after pushing nodes and freeing memory incorrectly?
What happens if we push nodes and then free the head pointer before printing? Consider this code:
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;
    push(&head, 100);
    push(&head, 200);
    free(head);
    printList(head);
    return 0;
}
AUndefined behavior or crash
B100 -> NULL
CNULL
D200 -> 100 -> NULL
Attempts:
2 left
💡 Hint
Freeing head only frees one node, but list still points to freed memory.
🧠 Conceptual
expert
2:00remaining
Why must push use a pointer to pointer for head?
In linked list push operations, why do we pass a pointer to the head pointer (struct Node**) instead of just the head pointer (struct Node*)?
ABecause passing struct Node* is faster and more efficient.
BBecause we need to modify the head pointer itself to point to the new node.
CBecause struct Node* cannot be used to access node data.
DBecause pointer to pointer automatically frees memory.
Attempts:
2 left
💡 Hint
Think about what happens when you want to change the start of the list.