Stack — NCERT Solutions
Haryana Board · Class 12 · Computer Science
NCERT Solutions for Stack, Haryana Board Class 12 Computer Science: 10 textbook questions solved step by step.
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.

Learn better with visuals Super Tutor pairs illustrations like this with notes and quizzes for Stack.
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 operandsShow 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, in infix becomes 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:
| Step | Operation | List / Variable State |
|---|---|---|
| 1 | result = 0 | result = 0 |
| 2 | numberList = [10, 20, 30] | numberList = [10, 20, 30] |
| 3 | numberList.append(40) | numberList = [10, 20, 30, 40] |
| 4 | numberList.pop() returns 40 | result = 0 + 40 = 40; numberList = [10, 20, 30] |
| 5 | numberList.pop() returns 30 | result = 40 + 30 = 70; numberList = [10, 20] |
| 6 | print("Result=", result) | Prints Result= 70 |
Output:
Result= 702bFind 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:
| Step | Operation | Stack (answer) | output |
|---|---|---|---|
| 1 | answer=[] | [] | '' |
| 2 | answer.append('T') | ['T'] | '' |
| 3 | answer.append('A') | ['T','A'] | '' |
| 4 | answer.append('M') | ['T','A','M'] | '' |
| 5 | ch = answer.pop() → 'M' | ['T','A'] | 'M' |
| 6 | ch = answer.pop() → 'A' | ['T'] | 'MA' |
| 7 | ch = answer.pop() → 'T' | [] | 'MAT' |
| 8 | print("Result=", output) | — | 'MAT' |
Output:
Result= MATNote: 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:
- Take the input string.
- Push each character of the string onto the stack.
- Pop each character from the stack and concatenate to form the reversed string.
- 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 : OLLEHExplanation:
- 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: , show step-by-step process for matching parentheses using stack data structure.Show solution
Given Expression:
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:
| Step | Character Scanned | Operation | Stack Contents |
|---|---|---|---|
| 1 | ( | PUSH ( | ( |
| 2 | ( | PUSH ( | ( ( |
| 3 | 2 | Ignore (operand) | ( ( |
| 4 | + | Ignore (operator) | ( ( |
| 5 | 3 | Ignore (operand) | ( ( |
| 6 | ) | POP → matches ( | ( |
| 7 | * | Ignore (operator) | ( |
| 8 | ( | PUSH ( | ( ( |
| 9 | 4 | Ignore (operand) | ( ( |
| 10 | / | Ignore (operator) | ( ( |
| 11 | 2 | Ignore (operand) | ( ( |
| 12 | ) | POP → matches ( | ( |
| 13 | ) | POP → matches ( | Empty |
| 14 | + | Ignore (operator) | Empty |
| 15 | 2 | Ignore (operand) | Empty |
Conclusion: At the end of the scan, the stack is empty. Therefore, all parentheses in the expression are properly matched and balanced.
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
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 Haryana Board Class 12 Computer Science?
Are these NCERT Solutions for Stack free?
How should I revise Stack for the Haryana Board Class 12 board exam?
Sources & Official References
Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.
More resources for Stack
Practice Quiz
Test yourself with a quick quiz
Important Questions
Exam-style questions with answers
Revision Notes
Key points for last-minute revision
Formula Sheet
The chapter's formulas in one place
Chapter Summary
Understand the chapter at a glance
Concept Maps
See how topics connect
Study Plan
Step-by-step plan for this chapter
Flashcards
Quick-fire cards for active recall
Syllabus
What topics to cover
For serious students
Get the full Stack chapter — start free.
Quizzes, flashcards, an AI doubt solver and a study plan for Haryana Board Class 12 Computer Science. Free to start, no card needed.