1 Answers
Difference Between Stack and Queue
Stack: A stack is a data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed.
Queue: In contrast, a queue follows the First In, First Out (FIFO) principle. The first element added to the queue is the first one to be removed.
Example Scenario:
When to use a stack over a queue:
- Scenario: Implementing an undo feature in a text editor. When a user performs an action, it is added to the stack. To undo the action, the most recent action (last added) is popped from the stack.
When to use a queue over a stack:
- Scenario: Processing data in the order of arrival. For example, in a print queue, documents are printed in the order they are added to the queue, following the FIFO principle.
Please login or Register to submit your answer