💻 Computer Science · Topic 50 of 80

Stacks and Queues

Stacks are last-in first-out; queues are first-in first-out.

Interactive labFollow the idea step by step

About this topic

A stack and a queue are data structures that control the order in which items are added and removed. A stack is last in, first out (LIFO), like a pile of plates: push adds to the top, pop removes from the top, and peek looks at the top. A queue is first in, first out (FIFO), like a line at a shop: enqueue adds at the back and dequeue removes from the front.

Stacks are used for undo features, browser back buttons, checking balanced brackets, and the call stack that tracks function calls. Queues are used for print jobs, messages waiting to be processed, and customers on a help line. A variation, the priority queue, serves the most important item first, as in hospital triage.

For example, pushing 1, 2, 3 onto a stack and popping gives 3 first, but adding them to a queue and removing gives 1 first. Both can be built with arrays or linked lists, and the main operations take constant time. Common errors include popping from an empty stack, called underflow, or adding to a full structure, called overflow. Always check for emptiness before removal.

Key ideas

  1. Stack: push and pop
  2. Queue: enqueue and dequeue
  3. Used in undo and printing

Quick check

Use cases: where Stacks and Queues helps

Computing ideas run phones, search engines, games and the wider internet.

Frequently asked questions

What is Stacks and Queues?

Stacks are last-in first-out; queues are first-in first-out.

What will I learn in Stacks and Queues?

Stack: push and pop. Queue: enqueue and dequeue. Used in undo and printing.

Is Stacks and Queues part of Computer Science?

Yes. It is one of 80 Computer Science topics on LearnViz HighSchoolEdu, with 3 key ideas, a live animation and a quick check.

Does the Stacks and Queues lab work on a phone or tablet?

Yes. The animation resizes to fit your screen and supports touch, so you can drag, tap and slide with your finger.

How should I study Stacks and Queues?

Read the summary and key ideas first, play with the interactive lab to see the idea in motion, then answer the quick check. If you get a question wrong, go back to the key idea it tests.

Do I need an account, and is my progress saved?

No account is needed. Marking a topic as learned is saved in your own browser, so it stays on that device and is not sent to a server.

More in Computer Science