Expression Evaluation Using Stacks

Introduction

Expressions like 3 + 4 * (2 - 1) are easy for people to read, but a computer cannot simply scan them left to right and get the right answer. Operators have different precedence levels, parentheses override those levels, and some operators associate left to right while others associate right to left. The stack solves this problem elegantly: whenever you encounter something whose handling must be postponed — an open parenthesis, an operator of lower precedence, an operand waiting for its operator — you push it and come back to it later, in exactly the reverse order you set it aside. That Last-In, First-Out behavior is what makes stacks the natural tool for parsing.

In this topic you will work through the chain of ideas that compilers and calculators actually use: checking that parentheses and brackets are balanced, understanding the three notations (infix, prefix, postfix), converting infix to postfix with the Shunting-Yard algorithm, and then evaluating a postfix expression with a single stack and a single left-to-right pass. The payoff is that postfix needs no parentheses and no precedence rules at evaluation time — all of that complexity is resolved during conversion.

As you watch the videos below, trace each example by hand with pencil and paper. Keep a column for the stack contents and a column for the output at every step, and pay close attention to the rules that govern when an operator is pushed versus when it forces pops: comparing precedence of the incoming operator against the top of the stack, handling equal precedence according to associativity, and treating the open parenthesis as a barrier that only a matching close parenthesis can remove.

Video 1: Lec-36: Infix, Prefix & Postfix👩‍🔧 with examples | Data Structure

Gate Smashers · 13:33 · 552,840 views · 3y ago · captions — Watch on YouTube

About this video

This lecture introduces the three ways of writing an expression — infix (operator between operands), prefix (operator first), and postfix (operator last) — and walks through worked examples converting between them. Based on its title and description, it should cover how operator position changes the reading order and why prefix and postfix forms need no parentheses.

Why this video

It establishes the vocabulary and notation that the rest of the topic depends on, so watching it first makes the conversion and evaluation videos much easier to follow.

Video 2: Lec-40: Infix to postfix Conversion using Stack | Infix➡️Postfix Conversion with examples

Gate Smashers · 15:15 · 650,693 views · 3y ago · captions — Watch on YouTube

About this video

This lecture demonstrates infix-to-postfix conversion using a stack, stepping through example expressions symbol by symbol. Expect it to show the standard rules: operands go straight to the output, operators are pushed or popped based on precedence comparisons with the stack top, and parentheses are handled as push-and-pop pairs.

Why this video

This is the core Shunting-Yard procedure stated in the module outcomes, presented as a hand-traceable algorithm with precedence and parenthesis handling made explicit.

Video 3: Lec-42: Postfix Expression Evaluation | Stack Application | Data Structure

Gate Smashers · 10:52 · 196,590 views · 3y ago · captions — Watch on YouTube

About this video

This lecture covers evaluating a postfix expression with a stack: scan left to right, push operands, and on each operator pop the required operands, apply the operation, and push the result back. It is framed explicitly as an application of the stack data structure, with numeric examples worked out.

Why this video

It completes the pipeline by showing what you do with the postfix string produced by the previous video, and it highlights the order-of-operands detail that trips students up when subtraction or division is involved.

How These Videos Support the Learning Outcomes

Together these three videos target MO4 directly: the first gives you the notation framework, the second shows the stack-based infix-to-postfix conversion including precedence, associativity, and parenthesis handling, and the third shows single-pass postfix evaluation with a stack. In doing so they also reinforce MO1, since every rule in these algorithms is a consequence of LIFO ordering, and they give you concrete algorithms to implement on top of the Stack class you build in MO2 using push, pop, peek, and isEmpty. Watching them with a trace table in hand will also prepare you to reason about MO3, because each algorithm makes a single pass over the input while the stack grows at most to the size of the expression.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.