Question 3 of 9intermediate🔍 AnalyzeShort Answer3 marks

Compare and contrast queue with stack.

Correct Answer

Queue and stack are both linear data structures, but they differ in how elements are inserted and deleted. In a queue, insertion and deletion take place from different ends — insertion (enqueue) happens at the rear end and deletion (dequeue) happens at the front end. Queue follows FIFO (First In First Out) order, meaning the element that enters first leaves first.

In contrast, a stack follows LIFO (Last In First Out) principle, where the most recently added element is removed first. For example, browser history uses a stack because when a tab is closed and reopened using ctrl+shift+T, the most recently closed URL opens first.

Both structures can be implemented using a deque, which allows insertion and deletion from either end.

Exercise: EXERCISE | Q: 2 | (Page 13)
For More Understanding

Explanation

The answer draws from the textbook context which describes queue as a linear list where insertion and deletion happen from different ends (rear and front respectively), following FIFO order. For stack, the context provides the browser history example demonstrating LIFO behavior. The comparison highlights the key difference: queue uses different ends for operations while stack uses the same end. The context also mentions that deque can support both stack and queue operations.