ProgrammingHard
A stack is implemented with a singly linked list, pushing at the head. The operations are push 1, push 2, pop, push 3, push 4, pop, pop. What is the sequence of popped values?
- A. 2, 3, 4
- B. 1, 4, 3
- C. 4, 2, 3
- D. 2, 4, 3
Correct answer: D. 2, 4, 3
The stack is LIFO. 2 is popped first, then 4, then 3, and 1 stays on the stack.
More C Programming questions
- A queue is kept as a singly linked list with head and tail pointers. Enqueue 1, 2, 3, dequeue twice, enqueue 4, then dequeue once. Which…
- Which loop correctly frees every node of a singly linked list whose head is p?
- A recursive function prints a list after its recursive call returns, as in print(n) { if (!n) return; print(n->next); putchar(n->v + '0')…
- In a 6-node list with values 10, 20, 30, 40, 50, 60, the standard slow and fast pointer method (slow moves one step, fast two steps, while…
- Which operation removes the k-th node (1-based) from a singly linked list in one pass with a pointer to the previous node, and handles…
- Which realloc pattern does not leak memory or lose the original block if the reallocation fails?