CBSE Class 12 Computer Science Chapter 9: Stack NCERT Solutions

NCERT Solutions PDF Class 12 PDF

This chapter provides comprehensive NCERT Solutions for Class 12 Computer Science, focusing on the fundamental data structure: Stack. It covers the essential concepts of stacks, including their LIFO (Last-In, First-Out) principle, and demonstrates how to perform various operations. The solutions meticulously explain the evaluation of postfix expressions, detailing the step-by-step status of the stack after each operation. This includes handling arithmetic and logical operators. These solutions are designed to help students understand the practical application of stacks in expression evaluation and prepare effectively for their board examinations by offering clear, concise, and accurate explanations.

Quick info

BoardCBSE
ClassClass 12
SubjectComputer Science
Session2026
LanguageEnglish
TypeNCERT Solutions
Chapter9. Stack

Chapter summary

Chapter 9 on Stacks for CBSE Class 12 Computer Science covers the LIFO data structure. The NCERT Solutions focus on evaluating postfix expressions, illustrating the stack's state with each operation. It includes examples with arithmetic and logical operators, providing a clear understanding of how stacks manage these computations. These solutions are crucial for grasping stack implementation and its role in expression processing.

Learning outcomes

  • Understand the LIFO principle of stacks.
  • Learn to perform push and pop operations on a stack.
  • Evaluate postfix expressions using a stack.
  • Trace the status of a stack during expression evaluation.
  • Apply stack operations to solve problems involving arithmetic and logical expressions.

Topics covered

Paper topics

  • Stack Data Structure
  • LIFO Principle
  • Stack Operations (Push, Pop)
  • Postfix Expression Evaluation
  • Arithmetic Operators in Postfix
  • Logical Operators in Postfix
  • Stack Status Tracking

Important topics

  • Postfix Expression Evaluation
  • Stack Operations
  • LIFO Principle
  • Tracking Stack Status

PDF preview

Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1

Evaluate the following postfix expression. Show the status of stack after execution of each operation separately: 2,13, +, 5, -6,3,/5,*,<
Solution:

We will evaluate the postfix expression 2,13, +, 5, -6,3,/5,*,< by processing it from left to right and using a stack to store operands. The status of the stack after each operation is shown below:

Item Processed Operation Stack Status (Bottom to Top)
2 PUSH 2 2
13 PUSH 13 2, 13
+ POP 13, POP 2. Evaluate 2 + 13 = 15. PUSH 15. 15
5 PUSH 5 15, 5
- POP 5, POP 15. Evaluate 15 - 5 = 10. PUSH 10. 10
6 PUSH 6 10, 6
3 PUSH 3 10, 6, 3
/ POP 3, POP 6. Evaluate 6 / 3 = 2. PUSH 2. 10, 2
5 PUSH 5 10, 2, 5
* POP 5, POP 2. Evaluate 2 * 5 = 10. PUSH 10. 10, 10
< POP 10, POP 10. Evaluate 10 < 10 which is FALSE. PUSH FALSE. FALSE

The final result of the expression evaluation is FALSE.

Question 2

Evaluate the following postfix expression: (show status of Stack after each operation) 100,40,8,/,20,10,-,+,*
Solution:

We evaluate the given postfix expression 100,40,8,/,20,10,-,+,* using a stack. Operands are pushed onto the stack, and when an operator is encountered, the required operands are popped, the operation is performed, and the result is pushed back. The stack's state is tracked after each step:

Item Processed Operation Stack Status (Bottom to Top)
100 PUSH 100 100
40 PUSH 40 100, 40
8 PUSH 8 100, 40, 8
/ POP 8, POP 40. Evaluate 40 / 8 = 5. PUSH 5. 100, 5
20 PUSH 20 100, 5, 20
10 PUSH 10 100, 5, 20, 10
- POP 10, POP 20. Evaluate 20 - 10 = 10. PUSH 10. 100, 5, 10
+ POP 10, POP 5. Evaluate 5 + 10 = 15. PUSH 15. 100, 15
* POP 15, POP 100. Evaluate 100 * 15 = 1500. PUSH 1500. 1500

The final result of the expression evaluation is 1500.

Question 3

Evaluate the following postfix expression. Show the status of stack after execution of each operation separately: T, F, NOT, AND, T, OR, F, AND
Solution:

We evaluate the postfix expression T, F, NOT, AND, T, OR, F, AND using a stack, processing operands and operators. 'T' represents True and 'F' represents False. The stack's state is tracked after each step:

Item Processed Operation Stack Status (Bottom to Top)
T PUSH T T
F PUSH F T, F
NOT POP F. Evaluate NOT F which is T. PUSH T. T, T
AND POP T, POP T. Evaluate T AND T which is T. PUSH T. T
T PUSH T T, T
OR POP T, POP T. Evaluate T OR T which is T. PUSH T. T
F PUSH F T, F
AND POP F, POP T. Evaluate T AND F which is F. PUSH F. F

The final result of the expression evaluation is F (False).

Common mistakes

  • Incorrectly applying the LIFO principle.
  • Errors in handling operator precedence during postfix evaluation.
  • Mismanaging the stack state (e.g., popping from an empty stack).
  • Confusing operand and operator roles in expression evaluation.

Revision tips

  • Practice tracing stack operations manually for each example.
  • Focus on understanding the order of operations in postfix expressions.
  • Re-solve the given examples without looking at the solutions.
  • Pay close attention to how operands and operators interact on the stack.

Practice MCQs

Q1. What is the primary principle governing a stack data structure?

Q2. In postfix notation, where do operators appear relative to their operands?

Q3. When evaluating a postfix expression, what action is taken when an operator is encountered?

Q4. What is the result of evaluating the postfix expression '2, 3, +, 5, -'?

Q5. If the stack contains [10, 5] and the operation is '-', what is the result?

Frequently asked questions

What is a stack in computer science?

A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Elements are added (pushed) and removed (popped) from the same end, known as the top of the stack.

How is a stack used to evaluate postfix expressions?

When evaluating a postfix expression, we scan the expression from left to right. Operands are pushed onto the stack. When an operator is encountered, the top two operands are popped, the operation is performed, and the result is pushed back onto the stack.

What does 'LIFO' mean in the context of a stack?

LIFO stands for Last-In, First-Out. This means the most recently added element to the stack will be the first one to be removed.

Why is it important to show the status of the stack after each operation?

Showing the stack status after each operation helps in understanding the step-by-step execution of the algorithm, verifying the correctness of the evaluation, and identifying potential errors in the process.

Can stacks be used for logical operations as well as arithmetic ones?

Yes, stacks can be used to evaluate expressions involving logical operators (like AND, OR, NOT) in a similar manner to arithmetic operators, by popping operands, applying the logical operation, and pushing the boolean result.

Content reviewed by the NCERT Help team. Editorial Team and update policy

NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.