Intern
What is the difference between a stack and a queue?
sobes.tech AI
Answer from AI
Stack (java.util.Stack) and Queue are two fundamental data structures with different principles of element access.
| Feature | Stack (Stack) | Queue (Queue) |
|---|---|---|
| Principle | LIFO (Last-In, First-Out) | FIFO (First-In, First-Out) |
| Access | Add and remove from one end (top of stack) | Add from one end (tail), remove from another (head) |
| Operations | push (add), pop (remove), peek (view top without removing) |
offer/add (add), poll/remove (remove), peek/element (view head without removing) |
| Examples of use | Function execution (call stack), depth-first search (DFS), undo operations | Background task processing, data buffers, breadth-first search (BFS) |