Stacks and Queues

A stack is a last-in-first-out structure (push/pop at one end) used for nesting and backtracking, while a queue is first-in-first-out (enqueue rear, dequeue front) used for ordered processing; both support O(1) operations.

Key formulas & points

Skim these first — then read the full notes below.

  • Stacks drive DFS, expression evaluation and backtracking
  • Queues drive BFS, scheduling and buffering
  • A deque allows O(1) insertion and removal at both ends

Topic details

Introduction

This topic covers the two restricted linear structures. You implement stacks and queues over arrays or lists, handle the wrap-around logic of a circular queue, and recognise which real problems (expression parsing, BFS/DFS, scheduling) map naturally onto each discipline.

Key relations & formulas

Formulas (Indian textbook notation)

  • stack:pushandpopatthetopLIFOorderstack: push and pop at the top - LIFO order

Formulas (Indian textbook notation)

  • queue:enqueueatrear,dequeueatfrontFIFOorderqueue: enqueue at rear, dequeue at front - FIFO order

Formulas (Indian textbook notation)

  • circularqueuefullwhen(rear+1)modcapacity==frontcircular queue full when (rear + 1) mod capacity = = front

Notation and sign conventions

Relation 1 —
stack:pushandpopatthetopLIFOorderstack: push and pop at the top - LIFO order

Formulas (Indian textbook notation)

  • stack:pushandpopatthetopLIFOorderstack: push and pop at the top - LIFO order
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
queue:enqueueatrear,dequeueatfrontFIFOorderqueue: enqueue at rear, dequeue at front - FIFO order

Formulas (Indian textbook notation)

  • queue:enqueueatrear,dequeueatfrontFIFOorderqueue: enqueue at rear, dequeue at front - FIFO order
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
circularqueuefullwhencircular queue full when

Formulas (Indian textbook notation)

  • circularqueuefullwhen(rear+1)modcapacity==frontcircular queue full when (rear + 1) mod capacity = = front
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Stacks and queues restrict where you may add and remove, and that restriction is exactly what makes them useful. A stack’s LIFO order mirrors nested structure — function calls, matched parentheses, undo history — so depth-first search and recursion are stack-based. A queue’s FIFO order preserves arrival sequence, ideal for fair scheduling, buffering and breadth-first search. Implemented on an array, a queue must wrap around to reuse freed slots, giving the circular queue whose full and empty conditions are the classic point of confusion. A deque generalises both by allowing access at both ends.

Assumptions and validity limits

State assumptions explicitly before using any relation for stacks and queues — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Data Structures viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Data Structures papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to stacks and queues.
4. Use equation 1:
stack:pushandpopatthetopLIFOorderstack: push and pop at the top - LIFO order
.
5. Use equation 2:
queue:enqueueatrear,dequeueatfrontFIFOorderqueue: enqueue at rear, dequeue at front - FIFO order
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Stacks and Queues appears in interviews and systems programming. In Indian it software curricula this topic is tested because it connects theory to organising data for efficient algorithms.
GATE and semester exams often combine stacks and queues with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use stacks and queues?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students confuse LIFO with FIFO when choosing a structure, and mishandle the circular-queue full-versus-empty distinction (both can look like front == rear without a count or sacrificed slot). Popping or dequeuing from an empty structure (underflow) is another common oversight.

Quick revision checklist

Before attempting stacks and queues problems, confirm you can:
1. Stacks drive DFS, expression evaluation and backtracking
2. Queues drive BFS, scheduling and buffering
3. A deque allows O(1) insertion and removal at both ends
Revise the solved examples in Schaum Ds — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Stack evaluation order

Problem

Push 1, 2, 3 onto a stack, then pop twice. What values come out and what remains?

Solution

Pops return 3 then 2 (LIFO order); the value 1 remains on the stack. The last element pushed is the first popped.

Conceptual check — Stacks and Queues

Problem

In a Data Structures semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of stacks and queues." What should a complete answer include?

📖 Standard books (India)

  • Schaum DsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus