Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this stack push and pop sequence?

Consider a stack implemented using a linked list. The following operations are performed in order:

  1. Push 10
  2. Push 20
  3. Pop
  4. Push 30
  5. Pop
  6. Pop

What is the printed output of the popped elements?

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

struct Node* top = NULL;

void push(int x) {
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = top;
    top = temp;
}

int pop() {
    if (top == NULL) return -1;
    int val = top->data;
    struct Node* temp = top;
    top = top->next;
    free(temp);
    return val;
}

int main() {
    push(10);
    push(20);
    printf("%d ", pop());
    push(30);
    printf("%d ", pop());
    printf("%d\n", pop());
    return 0;
}
A20 30 10
B10 20 30
C30 20 10
D20 10 30
Attempts:
2 left
💡 Hint

Remember that stack is Last-In-First-Out (LIFO). The last pushed element is popped first.

🧠 Conceptual
intermediate
1:00remaining
What is the time complexity of push and pop operations in a linked list based stack?

For a stack implemented using a linked list, what is the time complexity of the push and pop operations?

APush is O(1), pop is O(n)
BBoth push and pop are O(1)
CBoth push and pop are O(n)
DPush is O(n), pop is O(1)
Attempts:
2 left
💡 Hint

Think about how many nodes you need to traverse for push or pop.

🔧 Debug
advanced
1:30remaining
What error does this stack pop function cause?

Consider this pop function for a linked list stack:

int pop() {
    if (top == NULL) {
        printf("Stack Underflow\n");
    }
    int val = top->data;
    struct Node* temp = top;
    top = top->next;
    free(temp);
    return val;
}

What error will occur when pop is called on an empty stack?

ASegmentation fault (accessing NULL pointer)
BStack Underflow message printed and returns -1
CCompilation error due to missing return statement
DNo error, returns 0
Attempts:
2 left
💡 Hint

Look at what happens after the if condition when top is NULL.

Predict Output
advanced
2:00remaining
What is the output after these stack operations?

Given a linked list stack, the following code is executed:

push(5);
push(15);
push(25);
pop();
push(35);
pop();
pop();

What is the printed output of the popped elements?

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

struct Node* top = NULL;

void push(int x) {
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = top;
    top = temp;
}

int pop() {
    if (top == NULL) return -1;
    int val = top->data;
    struct Node* temp = top;
    top = top->next;
    free(temp);
    return val;
}

int main() {
    push(5);
    push(15);
    push(25);
    printf("%d ", pop());
    push(35);
    printf("%d ", pop());
    printf("%d\n", pop());
    return 0;
}
A15 35 25
B25 15 35
C35 25 15
D25 35 15
Attempts:
2 left
💡 Hint

Trace the stack top after each operation carefully.

🧠 Conceptual
expert
1:30remaining
What is the maximum number of nodes in a linked list stack after these operations?

Starting with an empty linked list stack, the following operations are performed:

push(1);
push(2);
pop();
push(3);
push(4);
pop();
pop();
push(5);
push(6);
pop();

What is the maximum number of nodes present in the stack at any point during these operations?

A2
B4
C3
D5
Attempts:
2 left
💡 Hint

Count the stack size after each push and pop operation.