Skip to main content
Chapter 3 of 13
NCERT Solutions

Stack — NCERT Solutions

Chhattisgarh Board · Class 12 · Computer Science

NCERT Solutions for Stack, Chhattisgarh Board Class 12 Computer Science: 10 textbook questions solved step by step.

105 questions68 flashcards5 formulas & key relations5 concepts

Interactive on Super Tutor

Studying Stack? Get the full interactive chapter.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.

Free trial, no card needed.

An infographic illustrating various data structures like String, List, Set, Tuple, Array, Linked List, Binary Trees, Heaps, Graph, Sparse Matrix, and highlighting linear vs. non-linear structures.
Super Tutor

Learn better with visuals Super Tutor pairs illustrations like this with notes and quizzes for Stack.

10 Questions Solved · 1 Section

The first 5 solutions are open to read. The other 5 are free with a Super Tutor account.

EXERCISE — Chapter 3: Stack (Computer Science Class 12)

1State TRUE or FALSE for the following cases:
a) Stack is a linear data structure
b) Stack does not follow LIFO rule
c) PUSH operation may result into underflow condition
d) In POSTFIX notation for expression, operators are placed after operands
Show solution

Given: Statements about Stack data structure.

Concept: Recall the fundamental properties of Stack.

a) TRUE — Stack stores elements in a linear (sequential) order; elements are arranged one after another.

b) FALSE — Stack strictly follows the LIFO (Last-In-First-Out) principle, meaning the last inserted element is the first to be removed.

c) FALSE — PUSH operation inserts an element onto the stack. It may result in overflow (when the stack is full), NOT underflow. Underflow occurs during the POP operation when the stack is empty.

d) TRUE — In POSTFIX (Reverse Polish) notation, operators are placed after their operands. For example, A+BA + B in infix becomes AB+AB+ in postfix.

2aFind the output of the following code:
```
result=0
numberList=[10,20,30]
numberList.append(40)
result=result+numberList.pop()
result=result+numberList.pop()
print("Result=", result)
```
Show solution

Given: Python code using a list as a stack.

Concept: append() pushes an element to the top (end) of the list; pop() removes and returns the topmost (last) element.

Step-by-step execution:

StepOperationList / Variable State
1result = 0result = 0
2numberList = [10, 20, 30]numberList = [10, 20, 30]
3numberList.append(40)numberList = [10, 20, 30, 40]
4numberList.pop() returns 40result = 0 + 40 = 40; numberList = [10, 20, 30]
5numberList.pop() returns 30result = 40 + 30 = 70; numberList = [10, 20]
6print("Result=", result)Prints Result= 70

Output:

Result= 70
2bFind the output of the following code:
```
answer=[]; output=''
answer.append('T')
answer.append('A')
answer.append('M')
ch=answer.pop()
output=output+ch
ch=answer.pop()
output=output+ch
ch=answer.pop()
output=output+ch
print("Result=", output)
```
Show solution

Given: Python code using a list as a stack of characters.

Concept: append() pushes characters onto the stack; pop() removes from the top (LIFO order), effectively reversing the order of insertion.

Step-by-step execution:

StepOperationStack (answer)output
1answer=[][]''
2answer.append('T')['T']''
3answer.append('A')['T','A']''
4answer.append('M')['T','A','M']''
5ch = answer.pop() → 'M'['T','A']'M'
6ch = answer.pop() → 'A'['T']'MA'
7ch = answer.pop() → 'T'[]'MAT'
8print("Result=", output)—'MAT'

Output:

Result= MAT

Note: The word 'TAM' was pushed letter by letter and popped in reverse order, giving 'MAT'.

3Write a program to reverse a string using stack.Show solution

Concept: A stack follows LIFO principle. If we push all characters of a string onto a stack one by one and then pop them all, we get the characters in reverse order.

Algorithm:

  1. Take the input string.
  2. Push each character of the string onto the stack.
  3. Pop each character from the stack and concatenate to form the reversed string.
  4. Display the reversed string.

Python Program:

# Program to reverse a string using Stack

def reverseString(inputStr):
    stack = []          # Stack implemented using list
    reversed_str = ''   # To store the reversed string

    # PUSH each character of the string onto the stack
    for ch in inputStr:
        stack.append(ch)

    # POP each character from the stack to get reversed string
    while len(stack) != 0:
        reversed_str = reversed_str + stack.pop()

    return reversed_str

# Main program
originalStr = input("Enter a string: ")
result = reverseString(originalStr)
print("Original String :", originalStr)
print("Reversed String :", result)

Sample Output:

Enter a string: HELLO
Original String : HELLO
Reversed String : OLLEH

Explanation:

  • For input HELLO, characters H, E, L, L, O are pushed onto the stack.
  • Stack state: ['H','E','L','L','O'] (O is at top).
  • Popping gives: O, L, L, E, H → reversed string = OLLEH.
4For the following arithmetic expression: ((2+3)∗(4/2))+2((2+3)*(4/2))+2, show step-by-step process for matching parentheses using stack data structure.Show solution

Given Expression: ((2+3)∗(4/2))+2((2+3)*(4/2))+2

Concept: To check matching parentheses using a stack:

  • Scan the expression left to right.
  • If an opening parenthesis ( is encountered → PUSH it onto the stack.
  • If a closing parenthesis ) is encountered → POP from the stack (it should match the opening parenthesis).
  • At the end, if the stack is empty → parentheses are balanced/matched.

Step-by-step process:

StepCharacter ScannedOperationStack Contents
1(PUSH ((
2(PUSH (( (
32Ignore (operand)( (
4+Ignore (operator)( (
53Ignore (operand)( (
6)POP → matches ((
7*Ignore (operator)(
8(PUSH (( (
94Ignore (operand)( (
10/Ignore (operator)( (
112Ignore (operand)( (
12)POP → matches ((
13)POP → matches (Empty
14+Ignore (operator)Empty
152Ignore (operand)Empty

Conclusion: At the end of the scan, the stack is empty. Therefore, all parentheses in the expression ((2+3)∗(4/2))+2((2+3)*(4/2))+2 are properly matched and balanced.

5aEvaluate the following postfix expression while showing status of stack after each operation given A=3, B=5, C=1:
AB+C∗AB + C*

Free with a Super Tutor account

5bEvaluate the following postfix expression while showing status of stack after each operation given A=3, B=5, C=1, D=4:
AB∗C/D∗AB * C / D *

Free with a Super Tutor account

6aConvert the following infix notation to postfix notation, showing stack and string contents at each step:
A+B−C∗DA + B - C * D

Free with a Super Tutor account

6bConvert the following infix notation to postfix notation, showing stack and string contents at each step:
A∗((C+D)/E)A * ((C + D)/E)

Free with a Super Tutor account

7Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Display the content of the Stack along with the largest odd number in the Stack. (Hint: Keep popping out the elements from stack and maintain the largest element retrieved so far in a variable. Repeat till Stack is empty)

Free with a Super Tutor account

5 more solved questions in Stack

They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.

Frequently Asked Questions

What are the important topics in Stack for Chhattisgarh Board Class 12 Computer Science?
Key topics in Stack include Core Idea of Stack, Operations on Stack, Stack Implementation in Python, Notations for Arithmetic Expressions. Study these first, then practise questions on each for the Chhattisgarh Board Class 12 board exam.
Are these NCERT Solutions for Stack free?
The first 5 of the 10 solutions on this page are open to read. The other 5 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise Stack for the Chhattisgarh Board Class 12 board exam?
Learn the core ideas first, then work through the 105 practice questions on Stack. Revise definitions regularly and use flashcards for quick recall before the exam.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

For serious students

Get the full Stack chapter — start free.

Quizzes, flashcards, an AI doubt solver and a study plan for Chhattisgarh Board Class 12 Computer Science. Free to start, no card needed.