0% found this document useful (0 votes)
3 views2 pages

Stack Operations in Python

The document outlines the implementation of stack data structures for managing student records, book records, and city information. It includes user-defined functions for pushing and popping records, checking if the stack is empty, and displaying top elements. Additionally, it provides a sample dictionary and demonstrates how to filter and manage city data based on state names.

Uploaded by

marutheeshmarx43
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)
3 views2 pages

Stack Operations in Python

The document outlines the implementation of stack data structures for managing student records, book records, and city information. It includes user-defined functions for pushing and popping records, checking if the stack is empty, and displaying top elements. Additionally, it provides a sample dictionary and demonstrates how to filter and manage city data based on state names.

Uploaded by

marutheeshmarx43
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

DATA STRUCTURE – STACKS

1. A list named as student contains following format of for students:


[student_name, class, city].
Write the following user defined functions to perform given operations on the stack
named ‘Record’:
(i) Push_record() – To pass the list
student= [ ['Rahul', 12,'Delhi'], [‘Kohli',11,'Mumbai'], ['Rohit',12,'Delhi'] ]
and then Push an object containing Student name AND Class of student belongs to
‘Delhi’ to the stack Record and display and return the contents of stack
(ii) Pop_record(Record) – To pop all the objects from the stack and at last display “Stack
Empty” when there is no student record in the stack. Thus the output should be: -
[“Rohit”,12]
[“Rahul”, 12]
Stack Empty
(iii) isEmpty(Record) - This function checks whether the stack is empty. If the stack is
empty, the function should return True, otherwise the function should return False.
Answer:

2. You have a stack named BooksStack that contains records of books.


Each book record is represented as a list containing book_title, author_name, and
publication_year.
Write the following user-defined functions in Python to perform the specified operations
on the stack BooksStack:
(I) push_book(BooksStack, new_book): This function takes the stack BooksStack and a
new book record new_book as arguments and pushes the new book record onto the
stack.
(II) pop_book(BooksStack): This function pops the topmost book record from the stack and
returns it. If the stack is already empty, the function should display "Underflow".
(III) peep(BookStack): This function displays the topmost element of the stack without
deleting it. If the stack is empty, the function should display 'None'.
Answer:

3. A dictionary, state_city contains the following details :


state_city={ "Tamil Nadu":"Chennai", "Karnataka": "Bengaluru", "Maharashtra": "Mumbai",
"Kerala": "Thiruvananthapuram", "Delhi": "New Delhi"}
Define the following functions with the given specifications :
(i) push_city(state_city): It takes the dictionary as an argument and pushes all the cities in
the stack CITY whose states are of more than 4 characters.
(ii) pop_city(): This function pops the cities and displays "Stack empty" when there are no
more cities in the stack.
Answer:
CITY=[]
def push_city(state_city):
for k,v in state_city.items():
if len(k) > 4:
[Link](v)
state_city = { "Tamil Nadu": "Chennai", "Karnataka":"Bengaluru", "Maharashtra": "Mumbai",
"Kerala": "Thiruvananthapuram", "Delhi": "New Delhi"}
push_city(state_city)
def pop_city():
while CITY:
print([Link]())
print("Stack empty")
pop_city() pop_city()

You might also like