
The difference between a stack and a queue is the order in which items come out. A stack follows LIFO: last in, first out, so the newest item leaves first. A queue follows FIFO: first in, first out, so the oldest item leaves first. Think of plates piled on a counter versus people waiting in a line.
That small change in removal order creates very different behavior. Stacks are useful when recent work must be revisited first, such as undo history or recursive calls. Queues are useful when work should be handled in arrival order, such as ordinary job scheduling or breadth-first search.
A stack exposes one active end, usually called the top. Push places an item on top, and pop removes the item from that same position. If you push a textbook, a notebook, and then a calculator, the calculator is popped first because it arrived last.
A queue has two roles at opposite ends. Enqueue adds an item at the back, while dequeue removes the item at the front. If three print jobs arrive in the order one, two, and three, a plain FIFO queue offers job one for processing first.
Both are abstract data types. Their defining feature is the permitted behavior, not the material underneath. A programmer can implement either one with an array, a linked structure, or another suitable collection while preserving the same LIFO or FIFO contract.

Suppose a small support tool receives three task cards in this order: T-17, fix the login error; T-18, compress the avatar image; T-19, export a receipt. Add all three before removing anything. This fixed input makes the effect of each rule easy to see.
In a stack, T-19 sits on top because it arrived last. Three pop operations return T-19, then T-18, then T-17. That is useful if each new task temporarily interrupts the previous one and the program must unwind the interruptions in reverse order.
In a queue, T-17 remains at the front because it arrived first. Three dequeue operations return T-17, then T-18, then T-19. That fits a simple first-come, first-served policy. The stored cards are identical in both cases; only the removal rule changes the result.

Choose a stack when the newest unfinished item should be handled next. An editor can push each change so undo reverses the latest action first. Parsers use stacks to match nested brackets and evaluate expressions. Recursive function calls also behave like a stack: the most recent call must finish before the earlier call can continue.
A stack also controls depth-first exploration. The search follows one path, stores alternatives, and returns to the most recent unfinished choice when it reaches a dead end. This is the same last-in, first-out logic in algorithmic clothing.
Choose a queue when older waiting work should be handled before newer work. Examples include basic printer jobs, incoming messages to be processed, and tasks awaiting a worker. In breadth-first search, a FIFO queue preserves layers: nodes at distance k are processed before newly discovered nodes at distance k plus one.

Well-designed stack push and pop operations are usually constant time. Queue enqueue and dequeue can also be constant time with a linked queue, circular buffer, or double-ended queue. A resizable array may occasionally copy elements when it grows, so its usual guarantee is amortized constant time rather than constant time for every single insertion.
The interface does not guarantee an efficient implementation. Python documentation, for example, notes that removing from the beginning of a list is slow because the remaining elements must shift. It recommends collections.deque for fast additions and removals at both ends. The lesson is to choose both the correct ordering rule and an implementation that supports it efficiently.
Ask one question: which waiting item should leave next? If the answer is the newest one, use a stack. If the answer is the oldest one, use a queue. If the answer is the most urgent one, neither plain structure expresses the rule; consider a priority queue instead.
The value 9 pops first because it was pushed last. The full pop order is 9, 7, 4.
Red dequeues first because it entered the FIFO queue first. The full order is red, green, blue.
A queue processes earlier-discovered nodes before later-discovered nodes, which keeps the traversal moving level by level from the starting node.
If data-structure diagrams still blur together, Snitchnotes can turn your own lecture slides or programming notes into clearer study notes, flashcards, quizzes, and audio review.
Carnegie Mellon University: Stacks and Queues
Princeton University: Bags, Queues, and Stacks
Notes, quizzes, podcasts, flashcards, and chat — from one upload.
Try your first note free