Chap-7 Data Structure NCERT
Chap-7 Data Structure NCERT
SCIENCE WITH
GUIDELINES TO NCERT
PYTHON -A
NCERT
NCERT Chapter 3: Stack
1. State TRUE or FALSE for the def pop(stack) :
following cases :
(a) Stack is a linear data if stack == []:
structure.
(b) Stack does not follow LIFO return
rule.
(c) PUSH operation may result into return stack.pop()
Condition.
underflow
(d) In POSTFIX notation for expression, operators def reverse (string):
are placed after operands. n=len(string)
Ans. stack = O
(a) True for i in range(n):
(b) False push(stack, string[i])
string =
(c) True only if memory is full, False otherwise
for i in range(n):
(d) True
string += pop(stack)
2. Find the output of the following code : return string
(a) result = 0
numberlist - [10,20,301 string = "This is it"
numberList.append (40) print ("String:", string)
result = result +numberList.pop() reversedStr =reverse(string)
result = result + numberList. pop ) print("Reversed String:", reversedstr)
print( "Result =", result)
String : This is it
(b) answer =[]; output = ""
Reversed String: ti si siht
answer.append('T') 4. For the following arithmetic
answer. append('A') expression :
(2 + 3) * (4/2)) + 2
answer. append("M')
ch = answer.pop() show step-by-step process for matching parenthss
using stack data structure.
output = Output+ch
ch = answer.pop) Ans. For matching parentheses, we can push in
the stack for each opening parenthesis of the
output = Output + ch
expression and pop from the stack for each
ch = answer. pop)
closing parenthesis.
output = Output + ch Then, in the end if:
print("Result =", output) " stack is empty
Ans. (a) Result = 70 ’ Parentheses are balanced
(b) Result = MAT " underflow
’ unbalanced - extra closing parenthesis
3. Write a program to reverse a
string using stack. unempty stack
Ans.
def push(stack, item):
’ nbalanced - extra opening parenthesis
stack.append(item) Given expression : (2 +3) *(4/2)) +2
apler7:
DATASTRUCTURES
315
(( 3 Push 3
( 5 Push 35
3*5=15
Pop twice,
Evaluate and 15
Pop Push back
) 15 1
1 Push
Push 15/1= 15
(( Pop twice,
Evaluate and 15
Push back
4
15 4
4 Push
Pop 15 * 4 = 60
Pop twice,
Evaluate and 60
3+5 =8
to + . Pop from
stack, add in
Pop twice and expression then
Evaluate Push this
and Push back
operator (-)
1 1 AB+C
Push
1*8 = 8 Higher pre
# cedance than
Pop twice, hence Push
Evaluate Push AB+ CD
back D
Pop all and add
End of Ans. AB+ CD*
expression to expression
End of exDression : Ans. 8
316
COMPUTER SCIENCE
WITH
(6) Given expression: A*(C+ D)/E) def oddStack (num): PYTHON
Scanning from left to right if num %2 ==1:
Symbol
Action Stack Postfix push(stack, num)
A Expression def GetLargest(stack):
A
Push elem =pop(stack)
large =elem
( Push
*(
while elem != None:
if large < elem :
( Push large =elem
elem =pop(stack)
AC return 1arge
n=
+
Push
+
int(input("HoW
many
stack = ]#empty stack numbers?")
large = -99
D
ACD for i in range(n):
Pop till one opering number =
bracket is popped and
add popped operator
ACD +
int(input("Enter number
oddStack(number)
")
to expression print ("Stack created
is ", stack)
Push
large =GetLargest(stack)
print ("Largest number in
ACD + E
stack", large)
) Pop till one opening
bracket is popped and ACD +E/ Sample run of the above
add popped operator program is :
to expression How many
End of Pop all and add to numbers ? 5
expres Enter number: 11
expression
sion AnsACD +E/* Enter number : 22 Only odd numbers get
Enter number 33 stored in the stack.
7. Write a program to create a Stack for Enter number : 44
odd nunmbers
out of all the numbersstoring only Enter number : 55
uSer. entered by the Stack created is [11, 33, 55]
Display the content of the Stack along with the Largest number in stack 55
largest odd number in the Stack.
(Hint. Keep popping out the
and maintain the largest elements from stack
avariable. Repeat till element retrieved so far in
Ans.
Stack is empty)
def push (stack,item):
stack.append(item)
def pop(stack):
if stack == []:
return
return stack.pop()