Stack
Class 12 · Computer Science
4 ideas in Class 12 Computer Science, explained in plain words, with games and quizzes to practise.
Stack and the LIFO Rule
Part of: Stack basics and operations
A stack is a line of items where both adding and removing happen at the same end, called the top. It follows the last-in-first-out rule: the item kept in most recently is the first one taken out, just like plates in a pile or bangles on a wrist.
Why it matters. Stacks quietly run undo buttons, browser back buttons and bracket checking. A learner who sees the pattern in daily piles will grasp why programs use stacks for reversing, remembering steps and matching pairs.
Key words
- Stack
- : A line of items added and removed at one end only.
- Top
- : The single end where items enter and leave a stack.
- LIFO Rule
- : The rule that the most recently added item leaves first.
- Linear Structure
- : An arrangement where items sit in a clear order, one after another.
Try it in two minutes
- Pile four books and take them off one by one. Ask which book leaves first.
- Type three lines in an editor and press undo twice. Ask which line returns.
- Open three pages in a browser and press back once. Ask which page shows.
Try at home. Ask your child to find three piles at home that behave like stacks and explain the rule for each.
Push Pop Overflow Underflow
Part of: Stack basics and operations
Push places a new item on the top and pop takes the top item away. Popping an empty stack causes underflow, and pushing onto a full stack causes overflow. In Python a list acts as the stack, and since lists grow freely, school programs meet underflow but almost never overflow.
Why it matters. Every stack program must guard the empty case, or it will crash on an extra pop. Learners who check before popping write sturdy programs for undo, history and bracket matching.
Key words
- Push
- : Adding a new item on the top of the stack.
- Pop
- : Removing the top item from the stack.
- Underflow
- : The error of removing from an empty stack.
- Overflow
- : The error of adding to a completely full stack.
Try it in two minutes
- Push three coins onto a plate and pop one. Ask what changed.
- Pop until the plate is empty, then try one more pop. Ask what should happen.
- Ask your child to state the rule for a safe pop.
Try at home. Ask your child to act out five pushes and three pops with coins and name the top coin at each step.
Infix Prefix Postfix Notations
Part of: Arithmetic notations with stacks
Infix writes operators between operands, as in school sums. Prefix writes each operator before its operands, and postfix writes each operator after its operands. Prefix and postfix need no brackets because the operator positions already fix the order of work.
Why it matters. Computers prefer postfix since one left-to-right pass is enough to evaluate it. Learners who convert fluently understand how compilers remove brackets and schedule operations.
Key words
- Infix Notation
- : Writing where each operator sits between its two operands.
- Prefix Notation
- : Writing where each operator sits before its operands.
- Postfix Notation
- : Writing where each operator sits after its operands.
- Precedence
- : The rank that decides which operator works first.
Try it in two minutes
- Write a sum with plus between two letters and name the notation.
- Move the plus before the letters and then after them. Ask which is which.
- Convert a bracketed sum with a stack holding only the operators.
Try at home. Ask your child to convert three short bracketed sums into postfix forms.
Evaluating Postfix Expressions
Part of: Arithmetic notations with stacks
To evaluate postfix, read left to right: push each number onto the tray, and on meeting an operator, pop two values, apply it and push the answer back. When the end arrives with exactly one value left, that value is the result. More or fewer leftovers mean the expression was invalid.
Why it matters. This single-pass method is how machines compute without brackets. Learners who trace it by hand trust the stack and can spot broken expressions quickly.
Key words
- Operand
- : A value that an operator works on.
- Operator
- : A sign that combines values into a new value.
- Evaluation
- : Working out the final value of an expression.
- Invalid Expression
- : A form that leaves the wrong count of values at the end.
Try it in two minutes
- Evaluate 4 5 plus on a tray and read the answer.
- Evaluate 3 4 2 times plus step by step.
- Finish one with a leftover pair and ask what went wrong.
Try at home. Ask your child to evaluate two short postfix forms with single-digit numbers.
What your child will practise
- Evaluating Postfix Expressions: Ocean Puzzle (Clue Puzzle)
- Infix Prefix Postfix Notations: Candy Quick Fire (Quick Fire)
- Push Pop Overflow Underflow: Desert Quick Fire (Quick Fire)
- Stack and the LIFO Rule: Desert Puzzle (Clue Puzzle)
- Stack and the LIFO Rule: Space Quick Fire (Quick Fire)
- Quiz: Evaluating Postfix Expressions (8 questions)
- Quiz: Infix Prefix Postfix Notations (8 questions)
- Quiz: Push Pop Overflow Underflow (8 questions)
- Quiz: Stack and the LIFO Rule (8 questions)