Stack or Queue? A Simple Way to Choose

A stack and a queue both hold items, but they express different kinds of work.

Choose a stack when the newest unfinished item should be handled first. Think undo history, nested brackets, backtracking, or depth-first traversal.

Choose a queue when items should be handled in arrival order. Think task processing, customer lines, event streams, or breadth-first traversal.

The deciding question is simple: should the next item be the most recent one or the oldest waiting one?

Find more data-structure guidance in How to Choose the Right Data Structure.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top