Unit 3: Stacks and Queues
I. Foundations of Stacks
A. Introduction to stacks
A stack is a linear data structure in which insertion and deletion occur only at one end, called the top, according to the Last In, First Out (LIFO) principle.
- LIFO rule: The most recently inserted element is removed first; if
10,20, and30are pushed in order,30is popped first. - Top: The variable or pointer
TOPidentifies the current last element; an empty stack has no valid top element. - Restricted access: Elements are inserted using
PUSHand removed usingPOP; arbitrary middle insertion or deletion is not a standard stack operation. - Core operations:
PUSH(x)inserts elementx.POP()removes and returns the top element.PEEK()orTOP()returns the top element without removing it.isEmpty()checks whether the stack contains no elements.
- Errors: Underflow occurs when popping an empty stack, while overflow occurs when pushing into a full fixed-size stack.
- Complexity: Push, pop, and peek normally require
O(1)time. - Applications: Stacks support function calls, recursion, undo operations, browser history, backtracking, syntax parsing, and expression processing.
II. Stack Representations and Operations
A. List representation of stacks
A list-based stack stores each element in a linked node and keeps a pointer to the node at the top.
- Node structure: Each node contains
DATA, the stored value, andNEXT, a pointer to the node below it. - Top pointer:
TOP = NULLrepresents an empty stack; otherwise,TOPpoints to the newest node. - Push linkage: A new node is linked before the current top:
- Set
NEW.NEXT = TOP. - Set
TOP = NEW.
- Set
- Pop linkage: Save
TOP, advanceTOPtoTOP.NEXT, and release the saved node. - Advantages: Capacity grows dynamically, and overflow occurs only when memory allocation fails.
- Limitations: Every element needs pointer storage, and noncontiguous nodes may have poorer cache performance.
- Cost: Insertion and deletion at the head both take
O(1)time.
B. Array representation of stacks
An array-based stack stores elements in consecutive memory locations and uses an integer index to identify the top.
- Representation: For an array
STACK[0...N-1], initializeTOP = -1, whereNis the capacity. - Empty condition:
TOP == -1. - Full condition:
TOP == N - 1. - Top element: When nonempty, the current element is
STACK[TOP]. - Advantages: Arrays provide compact storage, direct indexing, and strong cache locality.
- Limitation: A fixed array has finite capacity; resizing requires allocating and copying elements.
- Example: After pushing
4,7, and9, the array positions0,1, and2hold those values andTOP = 2.
C. Stack traversal
Stack traversal visits every element, usually from the top downward, without changing the logical contents.
- Array traversal: Visit indices from
TOPthrough0. - Linked traversal: Begin at
TOPand repeatedly followNEXTuntilNULL. - Order: A stack containing bottom-to-top values
A, B, Cis traversed asC, B, A. - Complexity: Visiting
nelements takesO(n)time andO(1)auxiliary space. - Preservation: Traversal should not modify
TOP; using repeated pops would destroy the stack unless elements were restored.
for i <- TOP downto 0
visit STACK[i]Here, i is the current index and visit processes an element.
D. Push operation
The push operation inserts a new element at the top while preserving LIFO order.
- Array procedure: Check for overflow, increment
TOP, and store the value atSTACK[TOP]. - Linked procedure: Allocate a node, store the value, point it to the old
TOP, and updateTOP. - Invariant: After a successful push, the inserted value is the element returned by the next pop.
- Complexity: Push takes
O(1)time; dynamic-array resizing may occasionally takeO(n).
PUSH(STACK, x)
if TOP = N - 1
report OVERFLOW
else
TOP <- TOP + 1
STACK[TOP] <- xHere, x is the inserted value and N is the array capacity.
E. Pop operation
The pop operation removes and returns the current top element.
- Array procedure: Check for underflow, save
STACK[TOP], decrementTOP, and return the saved value. - Linked procedure: Save the top node’s data, move
TOPtoTOP.NEXT, and deallocate the old node. - Underflow guard: If
TOP = -1for an array orTOP = NULLfor a list, no element can be removed. - Complexity: Pop requires
O(1)time.
POP(STACK)
if TOP = -1
report UNDERFLOW
else
x <- STACK[TOP]
TOP <- TOP - 1
return xHere, x temporarily stores the removed element.
III. Stack-Based Expression Processing
A. Arithmetic expressions
Arithmetic expressions combine operands, operators, and grouping symbols according to precedence and associativity rules.
- Operands: Values or variables such as
8,x, andtotal. - Operators: Common binary operators are
+,-,*,/, and exponentiation^. - Precedence: Parentheses are evaluated first, followed by exponentiation, multiplication or division, and addition or subtraction.
- Associativity:
+,-,*, and/are generally left-associative; exponentiation is commonly right-associative. - Infix form: Operators appear between operands, as in
A + B * C. - Stack role: Operators and parentheses are temporarily stored while parsing or evaluating expressions.
B. Polish notation
Polish notation removes the need for precedence rules and parentheses by fixing each operator’s position relative to its operands.
- Prefix notation: The operator precedes its operands; infix
A + Bbecomes+ A B. - Postfix notation: The operator follows its operands; infix
A + BbecomesA B +.
- Unambiguous structure: Infix
(A + B) * Cbecomes prefix* + A B Cand postfixA B + C *. - Evaluation direction: Prefix is commonly scanned right to left, whereas postfix is scanned left to right.
- Machine suitability: A stack can evaluate either form without searching for parentheses or repeatedly applying precedence rules.
C. Evaluation of expressions
A postfix expression is evaluated by pushing operands and applying each operator to the two most recent values.
- Operand rule: When a number is encountered, push it.
- Operator rule: Pop the right operand first, pop the left operand second, compute
left operator right, and push the result. - Final condition: A valid expression leaves exactly one value on the stack.
- Worked example: For postfix
5 2 3 * +, push5,2,3; apply*to obtain6; then apply+to obtain11. - Complexity: An expression containing
ntokens is evaluated inO(n)time using up toO(n)stack space. - Error detection: Too few operands, division by zero, or multiple remaining values indicate an invalid evaluation.
D. Transformation of expressions
Expression transformation uses an operator stack to convert infix notation into prefix or postfix notation.
- Operand handling: Append each operand directly to the postfix output.
- Operator handling: Before pushing an operator, pop operators having higher precedence, or equal precedence when the incoming operator is left-associative.
- Parentheses handling: Push
(; on encountering), pop to the output until(is found, then discard the pair. - Completion: After scanning the expression, pop all remaining operators to the output.
- Example: Infix
A + B * Cbecomes postfixA B C * +because*has higher precedence than+. - Prefix conversion: One method scans a suitably reversed infix expression, forms postfix, and reverses the result while handling parentheses and associativity carefully.
- Complexity: Each token is pushed and popped at most once, giving
O(n)time andO(n)auxiliary space.
IV. Queue Representations and Operations
A. Array representation of queues
An array queue stores elements in indexed locations and tracks removal and insertion positions using FRONT and REAR.
- Linear queue: Insertions advance
REAR, while deletions advanceFRONT. - Initial state: A common convention sets
FRONT = 0,REAR = -1, and element countCOUNT = 0. - Circular queue: Indices wrap around using modulo arithmetic:
- Next rear:
(REAR + 1) mod N. - Next front:
(FRONT + 1) mod N.
- Next rear:
- Full condition: With a count variable,
COUNT = N; the empty condition isCOUNT = 0. - Benefit of circularity: Vacated positions are reused, avoiding false overflow in a linear array queue.
B. List representation of queues
A linked queue uses nodes and maintains pointers to both the first and last elements.
- Pointers:
FRONTpoints to the next node to delete, andREARpoints to the newest node. - Empty state: Both
FRONTandREARareNULL. - Insertion: Attach the new node after
REARand updateREAR. - Deletion: Remove the node at
FRONTand advanceFRONT. - Final-node rule: After deleting the only node, set both pointers to
NULL. - Complexity: Both insertion and deletion take
O(1)time.
C. Queue traversal
Queue traversal visits elements from FRONT toward REAR, preserving First In, First Out order.
- Logical order: If
10,20, and30were inserted in sequence, traversal visits10,20,30. - Linked traversal: Start at
FRONTand followNEXTuntilNULL. - Circular-array traversal: Visit
COUNTpositions, repeatedly using(index + 1) mod N. - Complexity: Traversing
nelements requiresO(n)time. - Non-destructive requirement: Traversal uses temporary indices or pointers rather than repeated deletions.
D. Queue insertion
Queue insertion, also called enqueue, adds an element at the rear.
- Array enqueue: Check whether the queue is full, advance
REARcircularly, store the value, and incrementCOUNT. - Linked enqueue: Allocate a node; if empty, assign both pointers to it, otherwise connect it after
REAR. - Ordering guarantee: A newly enqueued element is removed only after all previously queued elements.
- Complexity: Enqueue takes
O(1)time.
REAR <- (REAR + 1) mod N
QUEUE[REAR] <- x
COUNT <- COUNT + 1Here, x is the inserted value and N is the capacity.
E. Queue deletion
Queue deletion, also called dequeue, removes and returns the element at the front.
- Array dequeue: Check for underflow, save
QUEUE[FRONT], advanceFRONTcircularly, and decrementCOUNT. - Linked dequeue: Save the front node’s value, advance
FRONT, and release the old node. - FIFO result: Among all current elements, the one inserted earliest is removed first.
- Empty transition: In a linked queue, deleting the last node also sets
REAR = NULL. - Complexity: Dequeue requires
O(1)time.
V. Specialized Queues
A. Priority queues
A priority queue removes elements according to priority rather than insertion time alone.
- Ordering rule: In a max-priority queue, the largest priority is removed first; in a min-priority queue, the smallest is removed first.
- Tie handling: Elements with equal priorities may follow FIFO order to provide stable service.
- Implementations:
- An unsorted array offers
O(1)insertion butO(n)deletion of the highest-priority item. - A binary heap offers
O(log n)insertion and deletion, withO(1)access to the highest-priority item.
- An unsorted array offers
- Example: Tasks
(A,2),(B,5), and(C,3)leave in the orderB,C,Ain a max-priority queue. - Applications: Priority queues support CPU scheduling, shortest-path algorithms, event simulation, and emergency service systems.
B. Deques
A deque, or double-ended queue, permits insertion and deletion at both the front and rear.
- Input-restricted deque: Insertion is allowed at only one end, but deletion is allowed at both ends.
- Output-restricted deque: Deletion is allowed at only one end, but insertion is allowed at both ends.
- Core operations:
insertFront,insertRear,deleteFront, anddeleteRear. - Implementation: Circular arrays and doubly linked lists support all four operations in
O(1)time. - Relationship to other structures: Restricting operations can make a deque behave as either a stack or a queue.
- Applications: Deques support sliding-window algorithms, palindrome checking, task scheduling, and maintaining recent-history buffers.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →