0% found this document useful (0 votes)
6 views1 page

Python Stack Operations for Books and Cities

The document outlines two pre-board questions related to stack operations in Python. The first question involves creating a stack to manage a list of books, allowing for pushing books with quantities greater than 20 and popping them from the stack. The second question focuses on a stack of city records, requiring functions to push, pop, and display city information.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Python Stack Operations for Books and Cities

The document outlines two pre-board questions related to stack operations in Python. The first question involves creating a stack to manage a list of books, allowing for pushing books with quantities greater than 20 and popping them from the stack. The second question focuses on a stack of city records, requiring functions to push, pop, and display city information.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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]())

You might also like