Zamil's CSE Directory

data structures and algorithms

Stacks and Queues: Controlling Order

Learn how LIFO and FIFO structures shape processing order in real software systems.

#dsa#stacks#queues#order#processing
dsa, stacks, queues, order, processing guides

In the previous chapter we looked at linked structures.

Now we explore two classic abstract data types that control processing order.


Stack: Last In, First Out

A stack follows LIFO.

The most recently added item is removed first.

Core operations:

  • push add item
  • pop remove latest item
  • peek inspect latest item

Common use cases:

  • function call stack
  • undo/redo history
  • expression evaluation

Queue: First In, First Out

A queue follows FIFO.

The earliest added item is removed first.

Core operations:

  • enqueue add item
  • dequeue remove earliest item
  • front inspect earliest item

Common use cases:

  • task scheduling
  • request buffering
  • print job handling

Visual Model

graph TD
  A[Stack LIFO] --> B[Newest leaves first]
  C[Queue FIFO] --> D[Oldest leaves first]

These structures are simple but extremely important across operating systems and backend services.


Implementation Note

Both stacks and queues can be implemented using arrays or linked structures.

The main concept is not implementation detail, but operation order semantics.


Key Ideas to Remember

  • Stacks enforce LIFO order.
  • Queues enforce FIFO order.
  • Both are abstract structures with common core operations.
  • They appear in call management, scheduling, and buffering.
  • Order constraints make many systems predictable and manageable.

What Comes Next

Stacks and queues manage order.

Next we learn a structure optimized for fast key-based access:

hash tables and hashing.