Bird
0
0
DSA Cprogramming~10 mins

Stack Implementation Using Linked List in DSA C - Interactive Practice

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

Complete the code to create a new node for the stack.

DSA C
struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = [1];
    node->next = NULL;
    return node;
}
Drag options to blanks, or click blank then click option'
A0
BNULL
Cnode
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the input data.
Assigning the node pointer itself.
2fill in blank
medium

Complete the code to check if the stack is empty.

DSA C
int isEmpty(struct Node* top) {
    return [1] == NULL;
}
Drag options to blanks, or click blank then click option'
Atop->next
Btop
Ctop->data
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Checking top->next instead of top.
Comparing data field to NULL.
3fill in blank
hard

Fix the error in the push function to correctly add a new node on top of the stack.

DSA C
void push(struct Node** top_ref, int data) {
    struct Node* node = newNode(data);
    node->next = [1];
    *top_ref = node;
}
Drag options to blanks, or click blank then click option'
A*top_ref->next
Btop_ref
C*top_ref
Dtop_ref->next
Attempts:
3 left
💡 Hint
Common Mistakes
Using top_ref instead of *top_ref.
Using top_ref->next which is invalid pointer access.
4fill in blank
hard

Fill both blanks to correctly pop the top element from the stack and return its data.

DSA C
int pop(struct Node** top_ref) {
    if (isEmpty(*top_ref))
        return -1;
    struct Node* temp = [1];
    int popped = temp->data;
    *top_ref = [2];
    free(temp);
    return popped;
}
Drag options to blanks, or click blank then click option'
A*top_ref
B(*top_ref)->next
Ctop_ref
D(*top_ref)
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning temp to top_ref instead of *top_ref.
Not updating *top_ref to the next node.
5fill in blank
hard

Fill in the blank to implement a peek function that returns the top element's data without removing it.

DSA C
int peek(struct Node* top) {
    if (top == NULL)
        return -1;
    return [1];
}
Drag options to blanks, or click blank then click option'
Atop->data
Btop->next
Ctop
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Returning top pointer instead of its data.
Returning top->next which is the next node, not the data.