Kundali IQ

Home › Free notes › Class 12 Computer Science › Stack

Stack – Class 12 Computer Science notes, important questions and MCQs

NCERT Class 12 Computer Science, chapter 3. Notes, key terms, important questions with answers and MCQs.

हिंदी में पढ़ें

Chapter overview

A stack is a linear data structure in which elements are added and removed from the same end, called the top. It follows the Last In First Out (LIFO) principle. The main operations are PUSH (insert) and POP (delete). Stacks can be implemented in Python using lists, and they are used in undo operations, function calls, browser history and expression evaluation, such as converting infix expressions to postfix and evaluating postfix expressions.

Stack and its operations

  • In a stack, the last element inserted is the first to be removed (LIFO), like a pile of plates.
  • PUSH adds an element at the top; trying to push onto a full fixed-size stack is overflow. POP removes the top element; trying to pop from an empty stack is underflow. PEEK or TOP returns the top element without removing it, and isEmpty checks whether the stack is empty.
  • In Python, a list is used: stack.append(x) for PUSH, stack.pop() for POP, stack[-1] for PEEK and len(stack) == 0 for isEmpty.

Applications and expressions

  • Applications: reversing a string, undo/redo in editors, back button in browsers, function call management and checking matching parentheses.
  • Expressions can be written in infix (A + B), prefix or Polish (+ A B) and postfix or reverse Polish (A B +) notation; postfix needs no brackets or precedence rules during evaluation.
  • Converting infix to postfix uses a stack for operators according to precedence; evaluating postfix pushes operands and, on reading an operator, pops two operands, applies the operator and pushes the result. For example, 5 3 + 2 * evaluates to 16.

Key terms

Stack
a linear data structure following LIFO
LIFO
Last In First Out
PUSH
the operation of inserting an element at the top of a stack
Underflow
trying to pop from an empty stack
Postfix notation
an expression in which the operator follows its operands

Important questions with answers

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

Overflow occurs when we try to push an element onto a stack that is already full (in a fixed-size stack). Underflow occurs when we try to pop an element from an empty stack. In Python lists, overflow rarely occurs because lists grow dynamically.

Write Python functions to PUSH and POP elements in a stack.3 marks

def push(stack, x): stack.append(x). def pop_item(stack): if len(stack) == 0: print('Underflow'); return None, else: return stack.pop(). The list's end acts as the top of the stack.

Explain how a stack is used to evaluate a postfix expression with an example.5 marks

In postfix notation, operators come after their operands, so the expression can be evaluated from left to right using a stack without brackets or precedence rules. The algorithm is: scan the expression from left to right; if the symbol is an operand, push it onto the stack; if it is an operator, pop the top two elements, where the first popped is the right operand and the second popped is the left operand, apply the operator, and push the result back; after the whole expression is scanned, the single value left in the stack is the result. Example: evaluate 4 5 + 3 2 - *. Read 4: push, stack [4]. Read 5: push, stack [4, 5]. Read +: pop 5 and 4, compute 4 + 5 = 9, push, stack [9]. Read 3: push, stack [9, 3]. Read 2: push, stack [9, 3, 2]. Read -: pop 2 and 3, compute 3 - 2 = 1, push, stack [9, 1]. Read *: pop 1 and 9, compute 9 * 1 = 9, push, stack [9]. The result is 9, which equals (4 + 5) * (3 - 2) in infix. If the stack has more than one element at the end, or an operator finds fewer than two operands, the expression is invalid. In Python, the stack can be a list using append() and pop().

Quick revision

  • LIFO principle.
  • PUSH, POP, PEEK, overflow, underflow.
  • Implementation using Python lists.
  • Infix, prefix, postfix; conversion and evaluation.

Exam tips

  • Remember: append() = PUSH, pop() = POP for a Python list stack.
  • In postfix evaluation, the second popped value is the left operand.

Practice MCQs

  1. A stack follows the principle

    1. FIFO
    2. LIFO
    3. random access
    4. priority order
    Show answer

    Answer: (b) LIFO. Last In First Out.

  2. Inserting an element into a stack is called

    1. POP
    2. PEEK
    3. PUSH
    4. ENQUEUE
    Show answer

    Answer: (c) PUSH. It adds at the top.

  3. Trying to remove an element from an empty stack causes

    1. overflow
    2. searching
    3. sorting
    4. underflow
    Show answer

    Answer: (d) underflow. There is nothing to pop.

  4. For a Python list used as a stack, the PUSH operation is

    1. insert(0, x)
    2. remove(x)
    3. append(x)
    4. sort()
    Show answer

    Answer: (c) append(x). Elements are added at the end.

  5. The postfix form of A + B * C is

    1. A B + C *
    2. A B C * +
    3. + A * B C
    4. A B C + *
    Show answer

    Answer: (b) A B C * +. * has higher precedence.

  6. Which application uses a stack

    1. printing jobs in order
    2. the undo feature of an editor
    3. a ticket counter queue
    4. round-robin CPU scheduling
    Show answer

    Answer: (b) the undo feature of an editor. The last action is undone first.

  7. The value of the postfix expression 6 2 / 3 * is

    1. 1
    2. 4
    3. 9
    4. 12
    Show answer

    Answer: (c) 9. 6 / 2 = 3, then 3 * 3 = 9.

  8. If 1, 2, 3 are pushed in order and then two POP operations are done, the remaining element is

    1. 3
    2. 2
    3. 1
    4. none
    Show answer

    Answer: (c) 1. 3 and 2 are popped first.

All chapters of Class 12 Computer Science

Written by Kundali IQ from standard, well-established facts to help with revision. This is not an official NCERT publication; always read your textbook too.