Stack Class 12 Computer Science: Chapter 3 PDF and Explanation

This is Chapter 3, Stack, of the NCERT Class 12 Computer Science textbook — the official English edition published on ncert.nic.in.

The chapter occupies 14 printed pages of the book (pages 39–52) and moves from the LIFO data structure and its PUSH and POP operations, to a working Python implementation, to the two stack algorithms used for arithmetic expressions: infix-to-postfix conversion and postfix evaluation.

The Stack Class 12 chapter PDF is right below, and the rest of this page walks through the sections and ideas inside it.

Download the Official Chapter PDF

Your class’s code samples, traces and figures all come from this one official file, so open it before you start studying the breakdown below: Download the Stack Class 12 Computer Science Chapter 3 PDF (NCERT official).

It is the same 14-page English edition your teacher uses, and having it open lets you follow every algorithm, example and figure while you work through this page.


What the chapter holds Count Where it is used
Printed pages 14
Sections in the chapter 9
Figures with NCERT captions 5
Tables 1
Worked examples 1 solved step by step in our NCERT Solutions
Exercise questions 7 answered in our NCERT Solutions
Official NCERT PDF Download the chapter PDF the chapter exactly as NCERT publishes it

Chapter at a Glance: What Is Inside

This table maps every section of the chapter to its place in the book, so you can jump straight to the idea you need. The chapter runs over 14 printed pages (pp. 39–52), with the theory in the first half and the practice Exercise on the final page.

Section Book page
Introduction p. 39
Stack p. 40
Applications of Stack p. 41
Operations on Stack: PUSH and POP p. 42
Implementation of Stack in Python pp. 43–45
Notations for Arithmetic Expressions p. 46
Conversion from Infix to Postfix Notation pp. 47–49
Evaluation of Postfix Expression pp. 49–50
Summary p. 51
Exercise p. 52

What This Chapter Covers, and Why the Order Matters

This section is a route map — read it once so you can see where the chapter is going before you study the details.

The chapter opens by recalling the Python data types you already know from Class XI — string, list, tuple, set — and points out what they share: several elements grouped together so that storage and access become faster. That grouping is a data structure, and when the elements sit in a sequence it is a linear data structure (NCERT, p. 39).

Stack and Queue are the two linear structures the book then introduces, and Stack comes first.

The middle of the chapter defines the stack itself. It starts from the piles you see at home — plates, books, stacked chairs — and shows that the only end you can touch is the top. From that come the LIFO rule, the two operations PUSH and POP, and the overflow and underflow conditions (NCERT, pp. 40–42).

This is the vocabulary section: every later page assumes these words.

Next comes the practical half: building the stack in Python. The chapter writes a program called glassStack using a plain list and six small functions, and its key point is a relief — because append() and pop() already act on the end of the list, you do not even need to declare a TOP variable (NCERT, pp. 43–45).

The final half shifts from stacks of data to stacks of operators. Infix expressions like x + y/z need BODMAS precedence; postfix expressions carry their evaluation order inside the string itself. Algorithm 3.1 converts infix to postfix, Algorithm 3.2 evaluates the postfix result, and Examples 3.1 and 3.2 trace both by hand (NCERT, pp. 46–50).

The chapter closes with a Summary on p. 51 and an Exercise on p. 52 — the practice set mapped onto the skills table below.

Key Concepts Explained

This is the teaching core of the page — each idea below is explained the way a teacher would walk you through it, with the book’s page numbers so you can follow along.

Stack and the LIFO rule (p. 40)

A stack is the formal name for something you already do every day: a pile where you can only reach the top. It is one of the two linear data structures this book introduces — the other is Queue — so the chapter first reminds you why grouped data matters.

Grouping elements in a deliberate way makes storage and access faster; the chapter calls such a grouping a data structure, and when the elements are organised in a sequence, a linear data structure (NCERT, p. 39). A stack is exactly that, with one rule added: you add and remove elements only at the same end, the top of the stack.

Because the newest element is always on top, the element inserted last is the first one removed — that is the LIFO (Last-In-First-Out) principle (NCERT, p. 40). The book’s image is a pile of plates: you can put another plate on top or take the top plate off, and nothing in the middle moves.

PUSH, POP, overflow and underflow (p. 42)

Every stack has exactly two operations — one to add, one to remove — and each has a named failure condition you must be able to distinguish.

PUSH inserts a new element at the top of the stack; POP removes the topmost element (NCERT, p. 42). The two limits are where students slip: pushing onto a full stack raises overflow, while popping an empty stack raises underflow (NCERT, p. 42).

In Python the asymmetry matters — as the book notes, a list has no fixed size, so a list-based stack will never be full while memory lasts, and overflow is not a condition you meet in the chapter’s programs (NCERT, pp. 43–44).

Real-life and programming applications (p. 41)

The chapter does not jump straight into code — it first shows that a stack is not an abstract invention but the model behind piles you see everywhere.

In everyday life: a pile of clothes in an almirah, chairs stacked vertically, bangles on a wrist, boxes of eatables on a kitchen shelf (NCERT, p. 41). In programming, the same structure does real work:

  • Reversing a string — push the characters, then pop them back in reverse order.
  • Undo and redo in text or image editors — the most recent edit is the first one undone.
  • Browser Back button — pages visited as P1 → P2 → P3 come back as P3 → P2 → P1, the history held as a stack.
  • Checking matched parentheses — the compiler matches each opening parenthesis with a closing one and throws an error when the pairs do not nest correctly (NCERT, p. 41).

Building a stack from a Python list (pp. 43–45)

Python has no built-in stack type, but it does not need one: an ordinary list behaves like a stack when you use append() and pop().

The chapter fixes the rightmost end of the list as the top. append() adds an element at that end — that is PUSH — and pop() removes from that end — that is POP. Because both methods are built into the list, the book’s note is explicit: no explicit TOP variable is needed (NCERT, p. 43). The six functions of the glassStack program are:

Function What it uses What it does / returns
isEmpty(glassStack) len(glassStack) == 0 True if the stack is empty, else False
opPush(glassStack, element) glassStack.append(element) Pushes element onto the top; returns nothing
size(glassStack) len(glassStack) Number of elements in the stack
top(glassStack) glassStack[len-1] Topmost element; prints ‘Stack is empty’ and returns None if empty
opPop(glassStack) glassStack.pop() Removes and returns the top element; prints ‘underflow’ and returns None if empty
display(glassStack) loop from len-1 down to 0 Prints the stack from top to bottom

The running program pushes ‘glass1’ and ‘glass2’, pops ‘glass2’, pushes ‘glass3’, reads the top, displays the stack, then pops until the stack is empty — the full output trace is explained in the FAQ at the end of this page (NCERT, pp. 44–45).

Infix, prefix and postfix (p. 46)

The same arithmetic expression can be written in three notations; the difference is where the operator sits, and that changes what you must know to evaluate it.

Infix is what you write normally — operators between operands, like x + y, with parentheses and the BODMAS rule controlling the order. Prefix (Polish) notation places operators before their operands, an idea introduced by the Polish mathematician Jan Lukasiewicz in the 1920s. Postfix (Reverse Polish) notation places operators after their operands (NCERT, p. 46).

Because in prefix and postfix the positions of operations and operands determine the result, parentheses become unnecessary. The book’s Table 3.1 lists the three forms side by side (p. 46).

Notation Position of operators 3 * (4 + 5) written as
Infix between the operands 3 * (4 + 5)
Prefix (Polish) before the operands * 3 + 4 5
Postfix (Reverse Polish) after the operands 3 4 5 + *

Read the postfix form left to right: 3, then 4, then 5, then +, then *. The + appears before the *, so the addition must happen first — the writer’s intent is frozen into the order. That is why postfix needs no parentheses and why a computer evaluates it in a single left-to-right pass (NCERT, p. 47).

Tracing Algorithm 3.1: infix to postfix conversion (pp. 47–49)

This is the algorithm you will most often be asked to trace — converting an infix expression into postfix by hand. The one-line version: operands go straight out; operators wait on the stack in priority order.

Algorithm 3.1 (NCERT, p. 47) processes the expression one character at a time and does four things:

  • Left parenthesis: push it onto the stack.
  • Right parenthesis: pop operators into the output until the matching left parenthesis is popped; discard both parentheses.
  • Operator: while the top of the stack is an operator with precedence higher than or equal to its own, pop it into the output; then push the current operator.
  • Operand: append it directly to the output string.

Step 5 of the algorithm is easy to forget: after the last character, pop everything still on the stack into the output (NCERT, p. 47). Here is the full trace for (A + B) * (C – D), with the stack’s top at the right:

Character Action Stack (top at right) Postfix output
( Push the left parenthesis ( —
A Operand — append to output ( A
+ Operator — push (the ‘(‘ below blocks any popping) ( + A
B Operand — append to output ( + A B
) Right parenthesis — pop + into output, then pop ( and discard — A B +
* Operator — stack is empty, so push * A B +
( Push the left parenthesis * ( A B +
C Operand — append to output * ( A B + C
– Operator — push (the ‘(‘ below blocks any popping) * ( – A B + C
D Operand — append to output * ( – A B + C D
) Right parenthesis — pop – into output, then pop ( and discard * A B + C D –
end Step 5 — flush the stack: pop * into output — A B + C D – *

The converted expression is A B + C D – *. Notice the operands A, B, C, D never entered the stack — they moved straight to the output, which is why the book’s Summary says only operators are pushed during conversion (NCERT, p. 51).

Tracing Algorithm 3.2: postfix evaluation (pp. 49–50)

Evaluation is the mirror image of conversion: this time the stack stores numbers (operands), and every operator pulls two of them out.

Scan the postfix string left to right. An operand is pushed; an operator pops two values, applies itself, and pushes the result back. When the input ends, the single value left on the stack is the answer; if more than one value remains, the expression is invalid (NCERT, p. 49).

The one trap is operand order: the first value popped is the right operand and the second is the left, so the sequence 8 2 – computes \(8 – 2\), not \(2 – 8\) — the same order visible in Figure 3.4 (NCERT, p. 50).

Worked trace for 5 3 + 2 *:

Step Character Action Stack (bottom to top)
1 5 Operand — push 5
2 3 Operand — push 5, 3
3 + Pop 3 (right) and 5 (left), compute \(5 + 3 = 8\), push 8
4 2 Operand — push 8, 2
5 * Pop 2 (right) and 8 (left), compute \(8 \times 2 = 16\), push 16
6 end Single value on the stack — output it 16 (answer)

The evaluation gives \(16\), which you can verify the ordinary way: \(5 + 3 = 8\), then \(8 \times 2 = 16\).

Reading the Chapter’s Figures

Let us now use this algorithm to convert a given infix expression (x + y)/(z*8) into equivalent postfix expression using a stack.
Let us now use this algorithm to convert a given infix expression (x + y)/(z*8) into equivalent postfix expression using a stack. Source: NCERT

This section walks through each captioned diagram of the chapter — the visual evidence behind the ideas above. Follow each one with a finger on your book’s page.

Figure 3.1: Stack of plates and books (p. 40)

A tall pile of plates standing beside a stack of books, showing that items can only be added to or removed from the top of each pile
Figure 3.1: Stack of plates and books. Source: NCERT

The chapter’s opening image makes LIFO visible before any definition is written (NCERT, p. 40). With a pile of plates or books you can only place an object on top and only remove an object from the top — pulling a plate from the middle would topple the pile.

“Inconvenient” in real life becomes a rule in Computer Science: all insertion and deletion happen at the top, and that single rule is what makes a stack a stack.

Figure 3.2: PUSH and POP operations on the stack of glasses (p. 42)

Numbered glasses stacked one on top of another, with arrows showing a new glass being placed on top and a top glass being lifted off
Figure 3.2: PUSH and POP operations on the stack of glasses. Source: NCERT

Here the stack is drawn as numbered glasses so individual elements can be followed (NCERT, p. 42). The diagram pairs a PUSH — a new glass landing on top — with a POP — the topmost glass leaving first.

If you push glass 1, then glass 2, then glass 3, the first pop returns glass 3; the picture is the LIFO rule, and it is also the visual the chapter’s Python program (glassStack) is named after.

Figure 3.3: Conversion of infix expression (x + y)/(z*8) to postfix notation (pp. 48–49)

A row-by-row conversion table for the infix expression (x + y)/(z*8), tracking the scanned character, the stack of operators and the postfix string being built
Figure 3.3: Conversion of infix expression (x + y)/(z*8) to postfix notation. Source: NCERT

This is the worked table of Example 3.1 (NCERT, pp. 48–49). Every row is one character of the infix expression and shows what the algorithm does with it. Run your eye down the stack column: it holds only operators and parentheses.

The string column does the opposite — it collects the operands x, y, z and 8 in exactly the order they appear, and operators join the string only when a right parenthesis arrives or a new operator outranks one already sitting on the stack.

Figure 3.4: Evaluation of postfix expression 7 8 2 * 4 / + (p. 50)

A step-by-step evaluation table for the postfix expression 7 8 2 * 4 / +, showing the operand stack after each number or operator is processed
Figure 3.4: Evaluation of postfix expression 7 8 2 * 4 / +. Source: NCERT

This is Example 3.2, the mirror image of Figure 3.3: the stack now stores operands (NCERT, p. 50). Watch one column: 7, 8 and 2 arrive and are pushed; * pops 2 and then 8 — the first value popped is the right operand — and pushes 16; 4 is pushed; / pops 4 and 16, pushing 4; and + pops 4 and 7, pushing \(7 + 4 = 11\). The single value left at the end, 11, is the answer.

If the operand order were reversed, every division and subtraction in the chapter would come out wrong — this figure is where that trap becomes visible.

Definitions You Can Quote in the Exam

These are the terms the chapter names and expects you to use precisely — each is a one-line definition you can quote in an answer, with the page where the book states it.

Term Definition Book page
Data structure a mechanism to store, organise and access data, together with the operations that can be efficiently performed on it p. 39
Linear data structure a data structure in which elements are organised in a sequence p. 39
Stack a linear arrangement of elements in which insertion and deletion happen at the same end, the top p. 40
Top of the stack the end of the stack from which elements are added or removed p. 40
LIFO Last-In-First-Out — the element inserted last is the first one removed p. 40
PUSH the insertion operation that adds a new element at the top of the stack p. 42
POP the deletion operation that removes the topmost element of the stack p. 42
Overflow the exception raised when an element is pushed onto a full stack p. 42
Underflow the exception raised when an element is popped from an empty stack p. 42
Infix notation an expression form in which operators are placed between the operands p. 46
Prefix (Polish) notation an expression form in which operators are placed before their operands p. 46
Postfix (Reverse Polish) notation an expression form in which operators are placed after their operands p. 46

Common Mistakes Students Make With Stacks

These are the errors that cost marks in stack traces and stack programs — name the mistake before you make it.

Mistake Correct rule How to check your answer
Confusing overflow with underflow Overflow is a PUSH onto a full stack; underflow is a POP on an empty stack (NCERT, p. 42) Ask which operation you attempted: PUSH → overflow, POP → underflow
Declaring an explicit TOP variable in Python append() and pop() already act on the end of the list, so no TOP variable is needed (NCERT, pp. 43–44) Your program should declare no top counter at all
Pushing operands during infix-to-postfix conversion Only operators (and parentheses) go on the stack; operands append directly to the output string (NCERT, p. 51) After the trace, all operands appear in the output in their original left-to-right order
Forgetting to empty the stack when the expression ends Step 5 of Algorithm 3.1 flushes every remaining operator into the output after the last character (NCERT, p. 47) Your postfix string has exactly as many operators as the infix string had
Swapping operands during postfix evaluation The first value popped is the right operand, the second is the left — so 8 2 – computes \(8 – 2\) (NCERT, p. 50) Work 8 2 – by hand: you must get 6, never -6
Starting a trace with a dirty stack Every conversion and evaluation trace begins with an empty stack and an empty output string The first row of your trace shows both columns empty

How This Chapter Is Tested

The chapter’s seven exercise questions map onto four skills — core concepts, predicting list behaviour, stack traces, and writing programs — so you can revise your weak spots instead of redoing everything. The questions sit together on p. 52.

Question Skill it tests
Q1 Core concepts — true/false statements on linear structure, LIFO, underflow and postfix placement
Q2 Python list behaviour — predicting what append() and pop() produce, remembering that pop() both removes and returns
Q3 Program writing — reversing a string by pushing its characters and popping them back
Q4 Stack tracing — matching parentheses in ((2+3)*(4/2))+2 by pushing ‘(‘ and popping on each ‘)’
Q5 Stack tracing — postfix evaluation with A=3, B=5, C=1, D=4, showing the stack state after each operation
Q6 Stack tracing — infix-to-postfix conversion, showing the stack and the output string at every step
Q7 Program writing — a complete stack program that stores only odd numbers entered by the user and reports the largest

Trace questions (Q4–Q6) expect the working, not just the final answer — write the stack and the output string after every step, because that working is the answer. Textbook contents and the examinable syllabus are not always identical, so check the current official syllabus before deciding what to prioritise.

The Chapter’s Four Think and Reflect Questions

These four questions sit inside the chapter without printed answers — they look optional, but each one explains why a stack matters in a real system. They are exactly what a definition-only revision misses.

1. How does a compiler or interpreter handle function calls? (p. 41)

Each function call pushes a frame — the return address and the local state — onto the call stack, and each return pops that frame. That is why the most recently called function is always the first to finish, and why recursion never loses its place.

The book raises this on p. 41 to connect the LIFO rule to the recursion you already write.

2. How does an operating system keep track of free memory? (p. 41)

The question’s aim is not to design an OS but to spot a stack-like structure inside one. Memory is a pool from which blocks are handed out and returned, and the OS must record which blocks are free — the same “manage a collection under rules” idea a stack implements.

Answer by naming the collection, the rules, and the structure that fits.

3. Write an algorithm to convert infix to prefix. (p. 47)

Start from Algorithm 3.1 and change one thing at a time. Because prefix places operators before their operands, scan the infix expression from right to left instead of left to right. The same stack logic transfers with the direction flipped.

4. Write an algorithm to evaluate a prefix expression. (p. 50)

Mirror Algorithm 3.2. Scan the prefix string from right to left: operands are pushed, each operator pops two values and pushes its result, and the single value left is the answer. If you can trace Algorithm 3.2 forwards, you can write this one by running it backwards.

The Entire Chapter on One Page

This is the chapter’s Summary (p. 51) rewritten in plain lines — everything the book concludes, in its order.

  • A stack inserts and deletes elements at one end only — the top.
  • It follows the LIFO principle: the element inserted last is the first one out.
  • PUSH inserts at the top; POP removes the top; popping an empty stack is underflow.
  • In Python, append() and pop() on a list make a stack — no explicit TOP variable is needed.
  • Any arithmetic expression can be written in infix, prefix or postfix form.
  • A single left-to-right pass evaluates a postfix expression, because operators already sit in evaluation order (NCERT, p. 51).
  • Converting infix to postfix pushes only operators; evaluating postfix pushes only operands.

One mnemonic for the precedence the algorithms assume: Brackets first, then division and multiplication, then addition and subtraction — the BODMAS order the chapter relies on for infix expressions (NCERT, p. 46).

This listing is maintained for the 2026-27 academic session using the NCERT textbook information available to us. NCERT remains the authority for confirming the latest edition.

Use these pages to move around the book and the site. The Class 12 Computer Science notes page lists every chapter in order, so you can also move backward from Stack to the chapters that precede it.

  • Class 12 Computer Science notes — the full chapter set for this book.
  • Chapter 4: Queue — the next chapter in the book and the natural follow-up to Stack.
  • Data Handling Using Pandas – I — another Class 12 Computer Science chapter page on this site.
  • Class 12 hub — study material across all Class 12 subjects.
  • CBSE notes index — every class and subject covered on the site.

Reference: NCERT Class 12 Computer Science textbook, chapter 3, official edition on ncert.nic.in.

Sources and Data Verification

The chapter description, figures, algorithms and exercise list on this page refer to the NCERT Class 12 Computer Science textbook, Chapter 3 “Stack”, official English edition published on ncert.nic.in.

This page covers that single chapter of the Class 12 Computer Science book — not the full CBSE scheme of studies.

The listing is maintained for the current academic session using the NCERT textbook information available to us.

NCERT settles textbook editions and PDFs; CBSE settles curriculum, syllabus and examinations. Textbook contents and the examinable syllabus are not always identical — check the current official syllabus.

Frequently Asked Questions

Why does a stack follow the LIFO principle?

Because both insertion and deletion happen at the same end — the top of the stack. Whatever was placed last sits on top, so it is the first thing removed; that is the LIFO principle in one line (NCERT, p. 40). The piles of plates and books in Figure 3.1 behave exactly this way.

What is the difference between overflow and underflow in a stack?

Overflow is the exception raised when you PUSH onto a full stack — there is no room for the new element. Underflow is the exception raised when you POP from an empty stack — there is nothing to remove (NCERT, p. 42).

In the book’s Python implementation the distinction is lopsided: a list-based stack is never full while memory lasts, so underflow is the condition the chapter’s programs actually meet (NCERT, p. 44).

How is a stack implemented in Python without a top pointer?

By using a list and trusting its built-in methods: append() adds at the end of the list (PUSH), and pop() removes from the end (POP). Because both act on the same end, the book notes that no explicit TOP variable is needed (NCERT, pp. 43–44). The glassStack program is this idea in full — six functions built on len(), append() and pop().

How do I convert an infix expression to postfix using a stack?

Scan the infix expression left to right and apply the four cases of Algorithm 3.1 (NCERT, p. 47): operands go straight to the output string; a left parenthesis is pushed; a right parenthesis pops every operator into the output until the matching left parenthesis is discarded; and an operator pops top operators of equal or higher precedence before being pushed itself.

When the expression ends, pop whatever remains on the stack into the output.

How does a stack check matching parentheses in an expression?

Push every left parenthesis onto the stack as you scan. On each right parenthesis, pop: if the stack supplies a matching left parenthesis, the pairs are properly nested; if a pop finds no left parenthesis, the parentheses are mismatched and the compiler throws an error (NCERT, pp. 41, 47). Exercise Q4 of the chapter is exactly this trace.

What output does the glassStack program in the NCERT chapter produce, and why does it end with underflow?

The program pushes glass1 and glass2, reports a size of 2, pops glass2, pushes glass3, reports the top element as glass3, and displays the stack as glass3 followed by glass1.

The final while loop then pops glass3, pops glass1, and calls opPop once more — on the empty stack opPop prints an underflow message and returns None, so the loop prints “Stack is empty now” and breaks (NCERT, pp. 44–45).

Explore Class 12 Computer Science Books

  • Previous: Data Handling Using Pandas – I
  • Next: Queue

Related chapters:


Related

More from this section