Data Structure
Data Structure
Q1. A list contains following record of customer: (1/2 mark for defining correct function header
(1/2 mark for correct loop in function
[customer_name,Roomtype]
(1 mark for checking the condition)
Write the following function user defined function to perform (1 mark for appending)
given operation on the list name ‘Hotel’:
i) Push_cust()- To push customer name of those Ans2:
customers who are staying in ‘Delux’ Roomtype. Stack=[]
ii) Pop_cust()-To pop the names of the customer from def Push(Vehicle):
the stack and also display “underflow” when there
for veh_name in Vehicle:
are no customer in the stack.
if vehicle[veh_name] in (“TATA”, ”TaTa”, ”tata”,
For example:
“Tata”,”taTa”):
If the list with customer details follows:
[“Siddharth”, ”Delux”] stack.append(veh_name)
[“Rahul” , “Standard”]
[“Jerry” , “Delux”]
The stack should contain Q3.A list NList contains following record as list elements:
Jerry [City,Country,Distance from delhi]
Siddharth Each of these record are nested together to form a nested
The output should be: list.Write the following user defined function in python to
Jerry perform the specified.
Siddharth
Operation on the name stack travel.
Underflow
i)Push_element(NList): It takes the nested list as an
(1/2 mark for defining correct function header (Push_cust()) argument and pushes a list object containing name of the
(1/2 mark for correct loop in function Push_cust())
(1/2 mark for checking the condition and appending the data in Push_cust()) city and country which are not in india snd distance is less
(1/2 mark for defining correct function header (Pop_Cust())
(1/2 mark for correct loop in function (Pop_Cust())
than 3500 km from Delhi.
(1/2 mark for deleting and displaying data in (Pop_Cust()) ii)Pop_element():It pop the object from the stack and
display+ them.Also the function should display “Stack
Ans1: empty” if there are no element in the stack.
Hotel=[] For example: If the nested list contain the following data:
Customer=[[“Siddharth”, ”Delux”],[“Rahul” , Nlist=[[“newyork”,“USA”,”11735”],
“Standard”][“Jerry” , “Delux”]] [“Naypyidaw”,”Myanmar”,3219], [“Dubai”,”UAE”,2194],
def Push_cust(): [“London”,”England”,6693], [“Gangtok”,”India”,1580] ,
for i in customer: [“Columbo” ,”sri lanka”,3405]]
if i[1]==“Delux”: The Stack Should Contain:
Hotel.append(i[0]) [“Naypyidaw”,”Myanmar”]
def Pop_cust(): [“Dubai”,”UAE”]
while len(Hotel)>0: [“Columbo” ,”sri lanka”]
print(Hotel.pop()) Output should be:
else: [“Columbo” ,”sri lanka”]
print(“Underflow”) [“Dubai”,”UAE”]
[“Naypyidaw”,”Myanmar”]
Underflow
Data Structure
Govt. Model Sanskriti Senior Secondary school ,Bilaspur(Haryana)
ii)def PopBig():
Ans3: while len(BigNums)>0:
i.travel=[] print(BigNumsl.pop())
def Push_element(NList): else:
for i in NList: print(“underflow”)
if i[1]!=”india” and i[2]<3500:
travel.append([ i[0] , i[1] ])
ii. def Pop_element(): Q5. (A) You have a stack named BooksStack that contains
while len(travel)>0: records of books. Each book record is represented as a list
print(travel.pop()) containing book_title, author_name, and
else: publication_year.
print(“underflow”) Write the following user-defined functions in Python to
perform the specified operations on the stack BooksStack
Q4.Consider a list named Nums which contain random I. push_book (BooksStack, new_book): This function takes
integerWrite the following user defined functions in the stack BooksStack and a new book record new_book as
Python and perform the specified operations on a stack arguments and pushes the new book record onto the
named BigNums. stack.
(i) PushBig(): It checks every number from the list Nums II. pop_book (BooksStack): This function pops the topmost
and pushes all such numbers which have 5 or more digits book record from the stack and returns it. If the stack is
into the stack, BigNums. already empty, the function should display "Underflow".
(ii) PopBig (): It pops the numbers from the stack, III. peep (BookStack): This function displays the topmost
BigNums and displays them. The function should also element of the stack without deleting it. If the stack is
display "Stack Empty" when there are no more numbers empty, the function should display 'None'.
left in the stack.
For example: If the list Nums contains the following Ans:
data: I.def push_book (BooksStack, new_book):
Nums [213, 10025, 167, 254923, 14, 1297653, 31498, 386, Bookstack.append(new_book)
92765]
Then on execution of PushBig (), the stack BigNums II. def pop_book(BooksStack):
should store: while len(Booksstack)>0:
[10025, 254923, 1297653, 31498, 92765] print(BooksStack.pop())
And on execution of PopBig (), the following output else:
should be displayed: print(“underflow”)
92765
31498 III. def peep(BooksStack):
1297653 while len(BooksStack)>0:
254923 print(len(BooksStack)-1)
10025 else:
Stack Empty print(“None”)