DSA Lab

Stack and queue

A stack and a queue are lists with rules about where you may add and remove. A stack takes from the same end it adds to (last in, first out); a queue takes from the opposite end (first in, first out).

Stack and queue: how it works

In this tool: push, pop, enqueue, dequeue. Put in your own values and watch it change. Each operation runs one step at a time and says in plain words what happened, with its calculation worked out. The code is shown in Python, C, C++ or Java with the line that just ran marked, and an Understand it panel gives the idea, why it works, the cost of each operation, common mistakes and a short quiz.

Lessons that use it

  • Stacks and queues (Python)Last in, first out and first in, first out: two ways to line up work, and where each is used.
  • Stacks (Data Structures)Last in, first out: a stack on an array with push, pop, overflow and underflow.
  • Using stacks: brackets and postfix (Data Structures)Check that brackets match, evaluate postfix expressions, and turn infix into postfix.
  • Queues (Data Structures)First in, first out: a circular array queue that wraps around, and a linked queue with front and rear.
  • Stacks and queues (Interview Prep)Stacks for matching brackets and for monotonic-stack problems like the next greater element; queues and deques for breadth-first order and sliding-window maximums.