DATA STRUCTURES-STACK
1. When a stack is empty and an element's deletion is tried from the stack, it is called ____.
2. Write Addnew(customer) and Remove(customer) methods in Python to Add a new customer details
and Remove a customer from the database from a List of customer whose details are in the
format[customer id,cname,accno,balance], considering them to act as PUSH and POP operations of
the data structure stack.
3. You have a stack named Books 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 Books:
(I) push_book(Books, 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(Books): This function pops the topmost book recordfrom the stack and
returns it. If the stack is already empty, the function should display "Underflow".
(III) peep(Books): This function displays the topmost element of the stack without
deleting it. If the stack is empty, the function should display 'None'.
4. A list, NList contains following record as list elements: [City, Country, distance from Delhi] Each
of these records are nested together to form a nested list. Write the following user defined
functions in Python to perform the specified operations on the stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list object
containing name of the city and country, which are not in India and distance is less than 3500
km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays
them. Also, the function should display “Stack Empty” when there are no elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734], ["Naypyidaw", "Myanmar", 3219], ["Dubai", "UAE",
2194],
["London", "England", 6693], ["Gangtok", "India", 1580], ["Columbo", "Sri Lanka", 3405]]
The stack should contain: ['Naypyidaw', 'Myanmar'], ['Dubai', 'UAE'], ['Columbo', 'Sri Lanka']
5. Write python function Push(), Pop() and Display to implement the stack. The program will store
the Employee details i.e. Employee number, Employee name and Salary.
6. A dictionary, StudRec, contains the records of students in the following pattern: {admno: [m1,
m2, m3, m4, m5]}, i.e., Admission No. (admno) as the key and 5 subject marks in the list as the
value. Each of these records is nested together to form a nested dictionary. Write the following
user-defined functions in the Python code to perform the specified operations on the stack
named BRIGHT
(i) Push_Bright(StudRec): it takes the nested dictionary as an argument and pushes a
list of dictionary objects or elements containing data as {admno: total (sum of 5
subject marks)} into the stack named BRIGHT of those students with a total mark
>350.
(ii) Pop_Bright(): It pops the dictionary objects from the stack and displays them. Also,
the function should display “Stack is Empty” when there are no elements in the
stack. For Example: if the nested dictionary StudRec contains the following data: 3
StudRec={101:[80,90,80,70,90], 102:[50,60,45,50,40], 103:[90,90,99,98,90]} Thes Stack
BRIGHT
Should contain: [{101: 410}, {103: 467}] The Output Should be: {103: 467} {101: 410} If the
stack
BRIGHT is empty then display: Stack is Empty .
7. You have a stack named MovieStack that contains records of movies. Each movie record is
represented as a list containing movie_title, director_name, and release_year. Write the
following user-defined functions in Python to perform the specified operations on the stack
MovieStack:
(I) push_movie(MovieStack, new_movie): This function takes the stack MovieStack and a new
movie record new_movie as arguments and pushes the new movie record onto the stack.
(II) pop_movie(MovieStack): This function pops the topmost movie record from the stack and
returns it. If the stack is empty, the function should display "Stack is empty".
(III) peek_movie(MovieStack): This function displays the topmost movie record from the stack
without deleting it. If the stack is empty, the function should display "None".
8. (a) Write the definition of a user-defined function push_odd(M) which accepts a list of
integers in a parameter M and pushes all those integers which are odd from the list M into a
Stack named OddNumbers.
(b) Write the function pop_odd() to pop the topmost number from the stack and return it. If
the stack is empty, the function should display "Stack is empty".
(c) Write the function disp_odd() to display all elements of the stack without deleting them. If
the stack is empty, the function should display "None".
For example:
If the integers input into the list NUMBERS are: [7, 12, 9, 4, 15]
Then the stack OddNumbers should store: [7, 9, 15]
9.
10.
11. Write a function in Python, Push(KItem), where KItem is a dictionarycontaining the details of Kitchen
items–{Item:price}.The function should push the names of those items in a stack which have priceless than
100. Also display the average price of elements pushed into [Link] example: If the dictionary
contains the following data:
{"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
The stack should contain
Glass
Knife The output should be: The average price of an item is 55.0
12. Two list Lname and Lage contains name of person and age of person respectively. A list named Lnameage
is [Link] functions as details given below
(i) Push_na() :- It will push the tuple containing pair of name and age from Lname and Lage
whose age is above 50.
(ii) Pop_na() : It will remove the last pair of name and age and also print name and age of removed person. It should
also print “underflow” if there is nothing to remove.
For example the two lists has following data:
Lname=[„narender‟, „jaya‟, „raju‟, „ramesh‟, „amit‟, „Piyush‟]
Lage=[45,23,59,34,51,43]
After Push_na() the contains of Lnameage stack is [(„raju‟,59),(„amit‟,51)]
The output of first execution of pop_na() is The name removed is amit The age of person is 51
13. A dictionary stu contains rollno and marks of students.
Two empty list stack_roll and stack_mark will be used as stack. Two function push_stu() and pop_stu() is defined
and perfom following operation
(a) Push_stu() :- It reads dictionary stu and add keys into stack_roll and values into stack_marks for all students
who secured more than 60 marks.
(b) Pop_stu() :- It removes last rollno and marks from both list and print "underflow" if there is
nothing to remove For example stu={1:56,2:45,3:78,4:65,5:35,6:90} values of stack_roll and stack_mark after
push_stu() [3,4,6] and [78,65,90].
14. Consider a list named Nums which contains random [Link] the following user defined functions in Python and
perform the specified operations on a stack named BigNums.
(i) PushBig(): It checks every number from the list Nums and pushes all such numbers which have 5 or more digits into
the stack, BigNums.
(ii) PopBig(): It pops the numbers from the stack, BigNums and displays them. The function should also display "Stack
Empty" when there are no more numbers left in the stack.
For example: If the list Nums contains the following data:
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
Then on execution of PushBig(), the stack BigNums should store:
[10025, 254923, 1297653, 31498, 92765]
And on execution of PopBig(), the following output should be displayed:
92765
31498
1297653
254923
10025
Stack Empty
15. Pranay has created a dictionary containing 5 different car names and their prices as key –value pairs.
Write separate user defined functions to perform the following operations:
Push the keys and values(car name, price) of the dictionary into a stack, where the corresponding value (price) is
greater than or equal to 900000.
Pop and display the content of the stack ad make the stack to be empty at the end.
For example:If the sample content of the dictionary is as follows:
MyDict={“Maruthi”:750000, ”Hyundai”:850000,”Kia”: 900000,”Honda”:1050000,”Ford”:1080000}
The output of the program should be : Ford 1080000
Honda 1050000
Kia 900000