Bird
0
0
DSA Cprogramming~10 mins

Stack Using Linked List vs Array Stack Trade-offs in DSA C - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push an element onto an array-based stack.

DSA C
void push(int stack[], int *top, int value, int max_size) {
    if (*top < max_size - 1) {
        stack[++(*top)] = [1];
    }
}
Drag options to blanks, or click blank then click option'
Avalue
Btop
Cmax_size
Dstack
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of the value to push.
Not incrementing the top index before assignment.
2fill in blank
medium

Complete the code to create a new node for a linked list stack push operation.

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

Node* push(Node* top, int value) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    if (new_node == NULL) return top;
    new_node->data = [1];
    new_node->next = top;
    return new_node;
}
Drag options to blanks, or click blank then click option'
Avalue
Btop
CNULL
Dnew_node
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the pointer top instead of the value.
Forgetting to assign the data field.
3fill in blank
hard

Fix the error in the array stack pop function to return the popped value correctly.

DSA C
int pop(int stack[], int *top) {
    if (*top == -1) {
        return -1; // Stack empty
    }
    return stack[[1]];
}
Drag options to blanks, or click blank then click option'
A*top
B(*top)++
C--(*top)
D(*top)--
Attempts:
3 left
💡 Hint
Common Mistakes
Not decrementing top (e.g., using just `*top`).
Using pre-decrement `--(*top)` which accesses the wrong element.
4fill in blank
hard

Fill both blanks to check if the linked list stack is empty and return the top element's data.

DSA C
int peek(Node* top) {
    if (top == [1]) {
        return -1; // Stack empty
    }
    return top->[2];
}
Drag options to blanks, or click blank then click option'
ANULL
B0
Cdata
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top against 0 instead of NULL.
Returning the next pointer instead of data.
5fill in blank
hard

Fill all three blanks to implement a function that returns the current size of an array stack.

DSA C
int size(int *top) {
    return [1] + [2];
}

// Usage example:
// int current_size = size(&[3]);
Drag options to blanks, or click blank then click option'
A*top
B1
Ctop
Dstack
Attempts:
3 left
💡 Hint
Common Mistakes
Returning top without adding 1.
Passing the wrong variable to the function.