Class 12 Computer Science Stacks NCERT Exercise Solution


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:

www.mycstutorial.in | NCERT Exercise Solution of Ch 3 Stack Class 12 Computer Science

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 –`

SymbolStack, 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

SymbolActionIntermediate Process / OutputStack, Initially Stack is Empty [ ]
3PUSH(3) 3
5PUSH(5) 3    5
+POP twice, Evaluate and PUSH BackPOP( ) => 5
POP( ) => 3
3 + 5 = 8
PUSH(8)
3
[ ] #Empty  

8
1PUSH(1) 8    1
*POP twice, Evaluate and PUSH BackPOP( ) => 1
POP( ) => 8
8 * 1 = 8
PUSH(8)
8
[ ]  #Empty  

8
End of ExpressionPOP [ ]
Answer : 8
Evaluating Postfix Expression using Stack | mycstutorial.in

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

SymbolActionIntermediate Process / OutputStack, Initially Stack is Empty [ ]
3PUSH(3) 3
5PUSH(5) 3    5
*POP twice, Evaluate and PUSH BackPOP( ) => 5
POP( ) => 3
3 * 5 = 15
PUSH(15)
3
[ ] #Empty  

15
1PUSH(1) 15    1
/POP twice, Evaluate and PUSH BackPOP( ) => 1
POP( ) => 15
15 / 1 = 15
PUSH(15)
15
[ ]  #Empty  

15
4PUSH(4)15 4
*POP twice, Evaluate and PUSH BackPOP( ) => 4
POP( ) => 15
15 * 4 = 60
PUSH(60)
15
[ ]

60
End of ExpressionPOP POP( ) => 60[ ]
Answer : 60
Evaluating Postfix Expression using Stack | mycstutorial.in


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

SymbolActionStack, Initially Stack is Empty [ ]Postfix Expressions
AAppend to Postfix Expression  [ ]A
+PUSH(‘+’) +A
BAppend to Postfix Expression+A B
– have equal precedence to +. First POP(‘+’) then PUSH(‘-‘) –A B +
CAppend to Postfix ExpressionA B + C
** have higher precedence than -, PUSH ‘*’– *A B + C
DAppend to Postfix Expression– *A B + C D
End of ExpressionPOP all and add to Postfix Expression[ ]A B + C D * –
Evaluating Postfix Expression using Stack | mycstutorial.in

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

SymbolActionStack, Initially Stack is Empty [ ]Postfix Expressions
AAppend to Postfix Expression  [ ]A
*PUSH ‘*’*A
(PUSH ‘(‘* (A
(PUSH ‘(‘* ( ( –A
CAppend to Postfix Expression-* ( (A C
+PUSH ‘+’– * ( ( +A C
DAppend 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 +
EAppend 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 ExpressionPOP all and add to Postfix Expression[ ]A C D + E / * –
Evaluating Postfix Expression using Stack | mycstutorial.in

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:

Odd Integer Stack and Largest Odd Number | www.mycstutorial.in

NCERT Solution of Class 12 Computer Science


Class 12 Computer Science Notes / Study Material


Class 11 NCERT Solution – Computer Science


Class 12 NCERT Solution – Informatics Practices


Class 11 NCERT Solution – Informatics Practices


Leave a Comment

You cannot copy content of this page

Scroll to Top