You have a list of tasks that must be processed in the exact order they arrive. Which data structure is best to use?
Think about which data structure follows the 'first come, first served' rule.
A queue processes elements in the order they arrive (FIFO), which is perfect for task scheduling where order matters. A stack processes elements in reverse order (LIFO), which is not suitable here.
What is the printed output after executing the following stack operations?
Stack s = create_stack(); push(s, 10); push(s, 20); push(s, 30); int x = pop(s); print(x); int y = pop(s); print(y);
Remember that stack removes the last pushed item first.
Stack is LIFO. The last pushed item is 30, so pop returns 30 first, then 20.
What is the state of the queue after these operations?
Queue q = create_queue(); enqueue(q, 5); enqueue(q, 10); dequeue(q); enqueue(q, 15); print_queue(q);
Remember that dequeue removes from the front of the queue.
After enqueue 5 and 10, queue is 5 -> 10. Dequeue removes 5. Then enqueue 15 adds to the end. Final queue is 10 -> 15.
What error will this code produce?
Stack s = create_stack();
push(s, 1);
pop(s);
pop(s);Think about what happens when you pop more times than you push.
After popping once, the stack is empty. Popping again causes underflow error because there is nothing to pop.
You are designing an undo feature for a text editor. Which data structure is best suited and why?
Undo means reversing the last action first.
An undo feature needs to reverse the most recent action first, which is exactly how a stack works (LIFO). A queue processes in the order actions were done, which is not suitable.
