STACK in Python – Solution of Pre Board Questions
STACK Question Question
PB- 1 PB- 2
A list containing records of Books as: Each node of a stack named CITY contained the following information:
BList=[("Fireworks",45),("Meadows",12),("Space • Pin code of a city
Adventure",43),("The Ghost",89)] • Name of city
Write the following user-defined function to perform Write the following user-defined functions in Python to perform the
operations on a stack named Books to: specified operations on the stack CITY:
I. Push_element()- To push an Book item containing i) PUSH_CITY(CITY, new_city): This function takes the stack
Book Name and Quantity having more than 20 into CITY and a new city record new_city as arguments and
the stack. pushes the new city record onto the stack.
II Pop_element()- To pop the Book item from the ii) POP_CITY(CITY): This function pops the topmost city
record from the stack and returns it. If the stack is already
stack and display them. Also, display “Stack Empty”
empty, the function should display "Underflow".
when there are no elements in the stack.
iii) DISPLAY(CITY): This function takes the stack CITY and display all
its elements If the stack is empty, the function should display 'None'.
Solution I.
def push_element():
for x in BList:
if x[1] >20 :
[Link](x)
II.
def pop_element():
while(1):
if Books == []:
print('stack empty')
return
else :
print([Link]())