stack questions
stack questions
productive manner.
Data Structure
LIFO
Top
[6] You can replace any element position in the stack. (True/False)
[7] The peek operation refers to accessing/inspecting the top element in the stack.
(True/False)
[9] While popping the element from the stack, a condition will be raised, this condition is
known as ____________.
Push, Pop
[11] What do you mean by data structure? Explain your answer with a suitable
example.
[13] Enlist a few of the fields where you feel a stack is used in real life.
Ans.: The stack is used in many fields in our routine life. Some examples are:
1. Browsers History
2. Mobile Phone Call log
3. Tubewell boring machine
4. Undo and redo commands in software
[14] What are the basic operations that can be performed on the stack?
Ans.: There are two basic operations can be performed on the stack:
1. Push – Inserting an element
2. Pop – Deleting an element
[15] What are the underflow and overflow conditions?
Ans.: Underflow is the condition which occurs when stack is empty while trying to delete
elements. Overflow is the condition which occurs while inerting an element when
memory is exhausted.
[16] Write steps on how you implement stack?
def is_underflow(stk):
if stk==[]:
return True
else:
return False
def push(stk,e):
stk.append(e)
top = len(stk)-1
def pop_stack(stk):
if stk==[]:
return "UnderFlow"
else:
e = stk.pop()
if len(stk)==0:
top = None
else:
top = len(stk)-1
return e
def display(stk):
if stk==[]:
print("Stack is Empty")
else:
top = len(stk)-1
print(stk[top],"-Top")
for i in range(top-1,-1,-1):
print(stk[i])
def peek(stk):
if stk==[]:
return "UnderFlow"
else:
top = len(stk)-1
return stk[top]
def AddPlayer(player):
player.append(pn)
def DeletePlayer(player):
if player==[]:
else:
return player.pop()
[23] Vedika has created a dictionary containing names and marks as key-value
pairs of 5 students. Write a program, with separate user-defined functions to
perform the following operations:
1. Push the keys (name of the student) of the dictionary into a stack, where
the corresponding value (marks) is greater than 70.
2. Pop and display the content of the stack.
def push(stk,item):
stk.append(item)
def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]
for i in d:
if d[i]>70:
push(stk,i)
while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
Exercise
Question 1
Question 2(a)
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 2(b)
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 3
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
Output
Enter a string: Hello world
String: Hello world
Reversed String: dlrow olleH
Question 4
Question 5(a)
AB + C * = 35 + 1 *
Scanning from left to right :
Hence, AB + C * = 35 + 1 * = 8
Question 5(b)
Question 6(a)
Question 6(b)
Question 7
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
Output
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 *
6. Convert the following infix notations to postfix notations, showing stack and
string contents at each step. a) A + B - C * D b) 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)
Read 2 , ignore
Read + , ignore
Read 3 , ignore
Read ) , pop from stack
Read * , ignore
Read ( , push to stack
Read 4 , ignore
Read / , ignore
Read 2 , ignore
Read ) , pop from stack
Read ) , pop from stack
Read + , ignore
Read 2 , ignore No unmatched parentheses means the e
xpression is balanced.
b) A B * C / D * → 3 5 * 1 / 4 *
Output: A B + C D * -
b) A * (( C + D)/E)
Output: A C D + E / *