NCERT Exercise Solution – Chapter 3 : Stack
1. State TRUE or FALSE for the following cases:
a) Stack is a linear data structure
Answer: (a) True
b) Stack does not follow LIFO rule
Answer: (b) False
c) PUSH operation may result into underflow condition
Answer: (c) False.
Underflow term is use in case of POP i.e. try to delete element from stack, but stack is empty.
In case of PUSH, Overflow condition is occurs, when Stack is FULL
d) In POSTFIX notation for expression, operators are placed after operands
Answer: (d) True
2. Find the output of the following code:
a) result = 0
numberList = [10,20,30]
numberList.append(40)
result = result + numberList.pop()
result = result + numberList.pop().
print(“Result=”,result)
Answer: Result = 70
b)
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)
Answer: Result= MAT
3. Write a program to reverse a string using stack.
Answer:

Output:
enter a string : My name is Anjeev Singh. Welcome to mycstutorial.in Reverse String is ni.lairotutscym ot emocleW .hgniS veejnA si eman yM
4. For the following arithmetic expression:
((2+3)*(4/2))+2
Show step-by-step process for matching parentheses using stack data structure.
Answer: Rules for matching parentheses.
(a) PUSH in the stack, if encounter opening parenthesis ‘(‘.
(b) POP from the stack, if encounter closing parenthesis ‘)’
(c) At the end; if stack is empty – Then Parentheses are balanced, otherwise Parentheses are unbalanced.
Given Expression : ( ( 2 + 3 ) * ( 4 / 2 ) ) + 2
Scanning from left to right –`
Symbol | Stack, Initially Stack is Empty [ ] | Action |
[ ] | ||
( | ( | PUSH ‘(‘ |
( | ( ( | PUSH ‘(‘ |
2 | ( ( | Ignore |
+ | ( ( | Ignore |
3 | ( ( | Ignore |
) | ( | POP |
* | ( | Ignore |
( | ( ( | PUSH |
4 | ( ( | Ignore |
/ | ( ( | Ignore |
2 | ( ( | Ignore |
) | ( | POP |
) | [ ] | POP |
+ | [ ] | Ignore |
2 | [ ] | Ignore |
End of Symbol |
5. Evaluate following postfix expressions while showing status of stack after each operation given A = 3, B = 5, C = 1, D = 4
a) A B + C *
b) A B * C / D *
Answer: (a) A B + C *
A = 3, B = 5, C = 1
Replacing Literals with Values :
Expression : A B + C *
After Substitution Expression : 3 5 + 1 *
Scanning from left to right
Symbol | Action | Intermediate Process / Output | Stack, Initially Stack is Empty [ ] |
3 | PUSH(3) | 3 | |
5 | PUSH(5) | 3 5 | |
+ | POP twice, Evaluate and PUSH Back | POP( ) => 5 POP( ) => 3 3 + 5 = 8 PUSH(8) | 3 [ ] #Empty 8 |
1 | PUSH(1) | 8 1 | |
* | POP twice, Evaluate and PUSH Back | POP( ) => 1 POP( ) => 8 8 * 1 = 8 PUSH(8) | 8 [ ] #Empty 8 |
End of Expression | POP | [ ] | |
Answer : 8 |
Answer: (b) A B * C / D *
A = 3, B = 5, C = 1, D = 4
Replacing Literals with Values :
Expression : A B * C / D *
After Substitution Expression : 3 5 * 1 / 4 *
Scanning from left to right
Symbol | Action | Intermediate Process / Output | Stack, Initially Stack is Empty [ ] |
3 | PUSH(3) | 3 | |
5 | PUSH(5) | 3 5 | |
* | POP twice, Evaluate and PUSH Back | POP( ) => 5 POP( ) => 3 3 * 5 = 15 PUSH(15) | 3 [ ] #Empty 15 |
1 | PUSH(1) | 15 1 | |
/ | POP twice, Evaluate and PUSH Back | POP( ) => 1 POP( ) => 15 15 / 1 = 15 PUSH(15) | 15 [ ] #Empty 15 |
4 | PUSH(4) | 15 4 | |
* | POP twice, Evaluate and PUSH Back | POP( ) => 4 POP( ) => 15 15 * 4 = 60 PUSH(60) | 15 [ ] 60 |
End of Expression | POP | POP( ) => 60 | [ ] |
Answer : 60 |
6. Convert the following infix notations to postfix notations, showing stack and string contents at each step.
a) A + B – C * D
Answer: Infix Expression is : A + B – C * D
Scanning from Left to Right
Symbol | Action | Stack, Initially Stack is Empty [ ] | Postfix Expressions |
A | Append to Postfix Expression | [ ] | A |
+ | PUSH(‘+’) | + | A |
B | Append to Postfix Expression | + | A B |
– | – have equal precedence to +. First POP(‘+’) then PUSH(‘-‘) | – | A B + |
C | Append to Postfix Expression | – | A B + C |
* | * have higher precedence than -, PUSH ‘*’ | – * | A B + C |
D | Append to Postfix Expression | – * | A B + C D |
End of Expression | POP all and add to Postfix Expression | [ ] | A B + C D * – |
Postfix Expression is : A B + C D * –
b) A * (( C + D)/E)
Answer: Infix Expression is : A * (( C + D)/E)
Scanning from Left to Right
Symbol | Action | Stack, Initially Stack is Empty [ ] | Postfix Expressions |
A | Append to Postfix Expression | [ ] | A |
* | PUSH ‘*’ | * | A |
( | PUSH ‘(‘ | * ( | A |
( | PUSH ‘(‘ | * ( ( – | A |
C | Append to Postfix Expression | -* ( ( | A C |
+ | PUSH ‘+’ | – * ( ( + | A C |
D | Append to Postfix Expression | – * ( ( + | A C D |
) | POP till one opening bracket is popped, add popped operator to expression | – * ( | A C D + |
/ | PUSH ‘/’ | – * ( / | A C D + |
E | Append to Postfix Expression | – * ( / | A C D + E |
) | POP till one opening bracket is popped, add popped operator to expression | – * | A C D + E / |
End of Expression | POP all and add to Postfix Expression | [ ] | A C D + E / * – |
Postfix Expression is : A C D + E / *
7. Write 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)
Answer:

NCERT Solution of Class 12 Computer Science
- Interface Python with MySQL – Important Questions – Answer
- Class 12 Computer Science – Exception Handling in Python NCERT Exercise Solutions
- Class 12 Computer Science File Handling in Python NCERT Exercise solution
- Class 12 Computer Science Stacks NCERT Exercise Solution
- Class 12 Computer Science Queues NCERT Exercise Solution
- Class 12 Computer Science Sorting NCERT Exercise Solution
- Class 12 Computer Science Chapter 8 Database Concepts NCERT Exercise Solution
- Class 12 Computer Science Structured Query Language SQL NCERT Book Exercise Solution
- Class 12 Computer Science Ch 10 Computer Network NCERT Book Exercise Solution
- Class 12 Computer Science Ch 11 Data Communication NCERT Book Exercise Solution
Class 12 Computer Science Notes / Study Material
- Class 12 Computer Science KVS Study Material PDF
- Python Revision Tour I : Basics of Python – Notes
- Python Revision Tour – II : String, Lists, Tuples, Dictionary – Notes
- Working with Functions in Python – Download Note pdf
- Exception Handling in Python – Notes
- Using Python Libraries – Notes
- File Handling – Notes
- Data Structure: Linear List – Notes
- Data Structure: Stacks – Notes
- Data Structure: Queues Using List – Notes
- Data Structure : Searching and Sorting – Notes
- Computer Networks – Notes
- Data Communication – Notes
- Network Security Aspects – Notes
- Relational Databases – Notes
- Table Creating and Data Manipulation Commands – Notes
- Simple Queries in SQL – Notes
- SQL Functions , Grouping Records, Joins in SQL – Notes
- Interface Python with Mysql – Notes
Class 11 NCERT Solution – Computer Science
- Class 11 Computer Science Ch 10 Tuples and Dictionary in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 11 Societal Impacts NCERT Book Exercise Solution
- Class 11 Computer Science Ch 9 Lists in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 8 Strings in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 7 Functions in Python NCERT Book Exercise Solution
- Class 11 Computer Science Ch 6 Flow of Control NCERT Exercise Solution
- Class 11 Computer Science Chapter 4 Introduction to Problem Solving NCERT Solution
- Class 11 Computer Science Chapter 5 Getting Started with Python NCERT Solution
- Encoding Schemes and Number Systems NCERT Exercise solutions
- Ch 2 Emerging Trends NCERT Exercise Solution – Class 11 Computer Science
- Class 11 Computer Science – Chapter 1: Computer Systems
- Class 11 Computer Science NCERT Exercise Solution
Class 12 NCERT Solution – Informatics Practices
- Class 12 Informatics Practices Chapter 6 Societal Impacts NCERT Exercise Solution
- Class 12 Informatics Practices Chapter 5 Internet and Web NCERT Exercise Solution
- Class 12 Informatics Practices Unit 4 Plotting Data using Matplotlib NCERT Exercise Solution
- Class 12 Informatics Practices Chapter 3 Data Handling Using Pandas – II NCERT Exercise Solution
- Class 12 Informatics Practices Data Handling using pandas – I NCERT Solution
- Class 12 Informatics Practices- Ch.1 Querying & SQL Function – NCERT Solution
Class 11 NCERT Solution – Informatics Practices
- Class 11 Informatics Practices Chapter 8 Introduction to Structured Query Language SQL NCERT Exercise Solution
- Class 11 Informatics Practices Chapter 7 Database Concepts NCERT Exercise Solution
- Class 11 Informatics Practices Chapter 6 Introduction of NumPy NCERT Exercise Solution
- Class 11 Informatics Practices Chapter 5 Understanding Data NCERT Solution
- Class 11 Informatics Practices Chapter 4 Working with Lists and Dictionaries NCERT Solution
- Class 11 Informatics Practices Chapter 3 Brief Overview of Python NCERT Solution
- Class 11 Informatics Practices – Ch. 2 Emerging Trends NCERT Solutions
- Class 11 Informatics Practices – Ch 1. Computer System NCERT Solution
- Class 11 – Informatics Practices – NCERT Exercise Solution