Skip to content

Class 12 · Computer Science · Computer Science

Chapter 3: Stack Notes

The big idea

A stack is a linear data structure where insertion and deletion happen only at the top, following Last-In-First-Out; Python lists implement it with append and pop, and stacks help convert and evaluate postfix expressions.

Things to remember

  • A data structure organises data for efficient storage and operations. A stack is a linear data structure in which elements are added and removed at one end only, called TOP, so the last element inserted is the first out (LIFO).
  • Real-life stacks: plates, books, clothes, bangles. In computing: reversing a string, undo and redo in editors, the browser back button (page history), and checking matched parentheses in expressions.
  • PUSH inserts an element at the top; POP removes the top element. Pushing onto a full stack gives overflow, and popping from an empty stack gives underflow. Both operations change only the TOP.
  • A Python list works as a stack: append(element) is PUSH and pop() is POP, both at the right end, so no explicit TOP is needed. A list-based stack never overflows unless memory runs out.
  • Standard stack functions: isEmpty returns True when len of the stack is 0; size returns len; top returns the last element (stack[len minus 1]) without removing it; opPop prints underflow and returns None if the stack is empty.
  • Notations: infix puts the operator between operands (x + y), prefix before them (+xy, Polish) and postfix after them (xy+, reverse Polish). Prefix and postfix need no parentheses or precedence rules, so one left-to-right pass evaluates them.
  • Infix to postfix: scan left to right; operands go to the output string; push a left bracket; for a right bracket pop to the output until the left bracket; for an operator pop higher or equal precedence operators first, then push it. Pop everything at the end. Only operators are stacked.
  • Postfix evaluation: push each operand; on an operator pop two values, apply it (the first popped is the right operand) and push the result. The final single element is the answer. Example: 7 8 2 * 4 / + gives 11. Only operands are stacked.

Words to know

Tap a card to see what it means

Quick quiz

⭐ 0 / 5
  1. An empty stack goes through: push 5, push 8, pop, push 3, pop. What is on top of the stack now?

  2. What is the value of the postfix expression 10 2 8 * + 3 - ?

  3. Which is the postfix form of the infix expression (x + y) / (z * 8)?

  4. Assertion (A): A stack made with a Python list never shows the overflow condition in normal use. Reason (R): A Python list has no fixed size limit, so append() keeps adding until memory is exhausted. Choose the correct option.

  5. A user visits pages P1, P2, P3 and P4 in order, then clicks the back button twice. Which page is shown, assuming the history is kept as a stack?

Match the pairs

Match each stack term with its meaning.

Try this at home

Make a stack with a pile of books or plates (ask an adult first). Push and pop items in order, and write down the order they come out to see Last-In-First-Out. Then try reversing the word COMPUTER by stacking letter cards.

All Computer Science notesChapter page and exercisesComputer Science syllabus