Computer Science

Stack & Queue

Fundamental linear data structures enforcing LIFO and FIFO access patterns.

Stacks and Queues: Linear Data Structures

Concept Overview

Stacks and Queues are fundamental linear data structures that organize data sequentially. They restrict where elements can be added or removed, which defines their behavior. A Stack operates on a Last-In, First-Out (LIFO) principle, similar to a stack of plates where you can only add or remove from the top. A Queue operates on a First-In, First-Out (FIFO) principle, much like a line of people waiting for service, where new arrivals join at the back and those at the front are served first.

Mathematical Definition

We can formalize stacks and queues as abstract data types (ADTs) with a specific set of operations and axioms:

// Stack (LIFO) Axioms
Pop(Push(S, x)) = S
Top(Push(S, x)) = x
IsEmpty(CreateStack()) = true
IsEmpty(Push(S, x)) = false
// Queue (FIFO) Axioms
Front(Enqueue(CreateQueue(), x)) = x
Dequeue(Enqueue(CreateQueue(), x)) = CreateQueue()
Front(Enqueue(Enqueue(Q, x), y)) = Front(Enqueue(Q, x))
IsEmpty(CreateQueue()) = true

Key Concepts

Last-In, First-Out (LIFO)

The defining characteristic of a stack is LIFO. The last element added (pushed) to the stack will be the very first element to be removed (popped). This behavior makes stacks perfect for tracking state that needs to be reversed or backtracked.

First-In, First-Out (FIFO)

Queues strictly adhere to FIFO. Elements are added (enqueued) at the rear (tail) and removed (dequeued) from the front (head). This enforces fairness in scheduling and buffering, processing items in the exact order they arrived.

Time Complexity

Both data structures are highly efficient for their specific use cases. Pushing and popping from a stack, as well as enqueueing and dequeueing from a queue, operate in O(1) constant time. However, searching for a specific element or accessing an arbitrary index takes O(n) time, as it requires iterating through the structure.

Implementation Constraints

When implemented using fixed-size arrays, both structures can suffer from capacity limits (Stack Overflow / Queue Full). Queues implemented with arrays must also manage the "drifting" of the head and tail pointers, often resolved by using a circular array buffer or by utilizing linked lists which dynamically allocate memory.

Historical Context

The concepts of stacks and queues have roots in early computing. In 1946, Alan Turing used the idea of a stack (which he called a "bury" and "unbury" mechanism) for calling and returning from subroutines. In 1955, Friedrich L. Bauer and Klaus Samelson patented the concept of a stack for evaluating mathematical expressions in compilers.

Queues naturally emerged from operations research and scheduling theory long before computers existed (Erlang's queueing theory in 1909 for telephone networks). In computer science, they became essential as multiprogramming and time-sharing operating systems developed in the 1960s, requiring fair allocation of CPU time among waiting tasks.

Real-world Applications

Stack Applications:

  • Call Stack: Managing execution contexts, local variables, and return addresses for function calls in almost all programming languages.
  • Undo/Redo Mechanisms: Text editors and graphic design software maintain stacks of user actions to reverse them sequentially.
  • Expression Evaluation: Parsing and evaluating prefix, postfix (Reverse Polish Notation), and infix mathematical expressions.
  • Backtracking: Algorithms like Depth-First Search (DFS) or solving a maze rely on stacks to retrace their steps when hitting a dead end.

Queue Applications:

  • Task Scheduling: Operating systems use queues to manage processes waiting for CPU time or disk I/O.
  • Message Brokers: Systems like RabbitMQ or Kafka use queue structures to reliably pass messages between microservices in the exact order they were produced.
  • Breadth-First Search (BFS): Graph traversal algorithms use queues to explore nodes level by level.
  • Print Spooling: Sending multiple documents to a printer processes them in the order they were received.

Related Concepts

  • Graph Traversal — utilizes stacks for DFS and queues for BFS
  • Dijkstra's Algorithm — uses a specialized Priority Queue where elements are ordered by weight rather than arrival time
  • Dynamic Programming — often uses tabular representations conceptually linked to evaluating states sequentially

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Stack & Queue module.

Try Stack & Queue on Riano →

More in Computer Science