NCERT Solutions for Class 12 Computer Science Chapter 3 : Stack

NCERT Solutions for Class 12 Computer Science Chapter 3 describes the in-depth study of the stack data structure. A stack is considered the simplest data structure that maintains the data in Last In, First Out mode. This chapter begins with the basics of the stack and thereby its operations on stacks such as push, pop, and peek. Solutions explain how stacks work and how fundamental they are in many applications such as function call and the evaluation of expressions.

Download PDF For NCERT Solutions for Computer-Science Stack

The NCERT Solutions for Class 12 Computer Science Chapter 3 : Stack are tailored to help the students master the concepts that are key to success in their classrooms. The solutions given in the PDF are developed by experts and correlate with the CBSE syllabus of 2023-2024. These solutions provide thorough explanations with a step-by-step approach to solving problems. Students can easily get a hold of the subject and learn the basics with a deeper understanding. Additionally, they can practice better, be confident, and perform well in their examinations with the support of this PDF.

Download PDF

Access Answers to NCERT Solutions for Class 12 Computer Science Chapter 3 : Stack

Students can access the NCERT Solutions for Class 12 Computer Science Chapter 3 : Stack. Curated by experts according to the CBSE syllabus for 2023–2024, these step-by-step solutions make Computer-Science much easier to understand and learn for the students. These solutions can be used in practice by students to attain skills in solving problems, reinforce important learning objectives, and be well-prepared for tests.

Stack

Question 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.

(d) In POSTFIX notation for expression, operators are placed after operands.

 

Answer :

(a) True
Reason — A stack is a linear data structure following the Last In, First Out (LIFO) principle, where elements are arranged sequentially.

(b) False
Reason — A stack follows the Last In, First Out (LIFO) rule. In a stack, the last element added is the first one to be removed from the stack.

(c) False
Reason — When attempting to remove an element (POP operation) from an empty stack, it leads to an underflow condition, resulting in an exception being raised.

(d) True
Reason — In POSTFIX notation for expression, operators are placed after the corresponding operands.

 


Question 2 :

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

 

Answer :

Output

Result= 70

Explanation

The code initializes result to 0 and creates a list [10, 20, 30]. It appends 40 to the list and then performs two pop() operations on the list, which removes and returns the last element (40 and 30). These values are added to result, resulting in result being 70. Finally, the code prints "Result=" followed by the value of result, which is 70.

 


Question 3 :

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

 

Answer :

Output

Result= MAT

Explanation

The code initializes an empty list answer and an empty string output. It appends the characters 'T', 'A', and 'M' to the answer list. After appending, answer list becomes ['T', 'A', 'M'].
After that, three pop() operations are performed on answer, removing and returning the last element each time ('M', 'A', and 'T' respectively). These characters are concatenated to the output string. So, output string becomes MAT which is printed as the final output.

 


Question 4 :

Write a program to reverse a string using stack.

Answer :

def push(stack, item):

    stack.append(item)

def pop(stack):

    if stack == []:

        return

    return stack.pop()

def reverse(string):

    n = len(string)

    stack = []

    for i in range(n):

        push(stack, string[i])

    string = ""

    for i in range(n):

        string += pop(stack)

    return string

string = input("Enter a string: ")

print("String:", string)

reversedStr = reverse(string)

print("Reversed String:", reversedStr)

Output

Enter a string: Hello world

String: Hello world

Reversed String: dlrow olleH

 


Question 5 :

For the following arithmetic expression:

((2 + 3) * (4 / 2)) + 2

Show step-by-step process for matching parentheses using stack data structure.

 

Answer :

For matching parentheses, we can push in the stack for each opening parenthesis of the expression and pop from the stack for each closing parenthesis.

Scanning from left to right:

Symbol

Stack

Action

(

( ↑ — top

Push

(

((

Push

2

 

..........

+

..........

3

..........

)

(

Pop

*

 

..........

(

((

Push

4

 

..........

/

..........

2

..........

)

(

Pop

)

#Empty

Pop

+

 

..........

2

#Empty

..........

Empty stack => Balanced Parentheses

 


Question 6 :

Evaluate the following postfix expression while showing status of stack after each operation given A = 3, B = 5, C = 1, D = 4.

AB + C *

 

Answer :

AB + C * = 35 + 1 *

Scanning from left to right :

Symbol

Action

Stack

Intermediate

output

3

Push

3

 

5

Push

5 3

 

+

Pop twice and Evaluate and Push back

#Empty

3 + 5 = 8

8

1

Push

1 8

 

*

Pop twice, Evaluate Push back

#Empty

1 * 8 = 8

8

Hence, AB + C * = 35 + 1 * = 8

 


Question 7 :

Evaluate the following postfix expression while showing status of stack after each operation given A = 3, B = 5, C = 1, D = 4.

AB * C / D *

 

Answer :

AB * C / D * = 35 * / 4 *

Scanning from left to right :

Symbol

Action

Stack

Intermediate

output

3

Push

3

 

5

Push

5 3

 

*

Pop twice, Evaluate and Push back

#Empty

3 * 5 = 15

15

1

Push

1 15

 

/

Pop twice, Evaluate and Push back

#Empty

15/1 = 15

15

4

Push

4 15

 

*

Pop twice, Evaluate and Push back

#Empty

15 * 4 = 60

60

Hence, AB * C / D * = 35 * / 4 * = 60

 


Question 8 :

Convert the following infix notation to postfix notation, showing stack and string contents at each step.

A + B - C * D

 

Answer :

Given,

A + B - C * D

Scanning from left to right :

Symbol

Action

Stack

Postfix

Expression

A

   

A

+

Push in stack

+

B

   

AB

-

Equal precedence to +. Pop from stack, add in expression then push this operator(-)

-

AB+

C

   

AB+C

*

Higher precedence than (-), hence Push

* -

D

   

AB+CD

End of expression

Pop all and add to expression

#Empty

AB+CD*-

Postfix notation of A + B - C * D = AB+CD*-

 


Question 9 :

Convert the following infix notation to postfix notation, showing stack and string contents at each step.

A * (( C + D)/E)

 

Answer :

Given,

A * (( C + D)/E)

Scanning from left to right:

Symbol

Action

Stack

Postfix Expression

A

   

A

*

Push

*

(

Push

(*

(

Push

((*

C

   

AC

+

Push

+((*

D

   

ACD

)

Pop till one opening bracket is popped and add popped operator to expression

(*

ACD+

/

Push

/(*

E

   

ACD+E

)

Pop till one opening bracket is popped and add popped operator to expression

*

ACD+E/

End of expression

Pop all and add to expression

#Empty

ACD+E/*

Postfix notation of A * (( C + D)/E) = ACD+E/*

 


Question 10 :

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 the stack and maintain the largest element retrieved so far in a variable. Repeat till Stack is empty)

 

Answer :

def push(stack, item):

    stack.append(item)

def pop(stack):

    if stack == []:

        return

    return stack.pop()

def oddStack(num):

    if num % 2 == 1:

        push(stack, num)

def GetLargest(stack):

    elem = pop(stack)

    large = elem

    while elem != None:

        if large < elem:

            large = elem

        elem = pop(stack)

    return large

n = int(input("How many numbers?"))

stack = []

for i in range(n):

    number = int(input("Enter number:"))

    oddStack(number)

print("Stack created is", stack)

bigNum = GetLargest(stack)

print("Largest number in stack", bigNum)

Output

How many numbers?8

Enter number:1

Enter number:2

Enter number:3

Enter number:4

Enter number:5

Enter number:6

Enter number:7

Enter number:8

Stack created is [1, 3, 5, 7]

Largest number in stack 7


Enquire Now