# Class 12 Computer Science Stacks NCERT Exercise Solution

#### 1. State 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

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

#### 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)

b)
output = output + ch
output = output + ch
output = output + ch
print(“Result=”,output)

#### 3. Write a program to reverse a string using stack.

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))+2Show step-by-step process for matching parentheses using stack data structure.

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

#### 5. Evaluate following postfix expressions while showing status of stack after each operation given A = 3, B = 5, C = 1, D = 4a) 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

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

#### a) A + B – C * D

Answer: Infix Expression is : A + B – C * D

Scanning from Left to Right

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

Postfix Expression is : A C D + E / *

#### 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)