VELAMMAL VIDHYASHRAM, SURAPET
WORKSHEET -DATA STRUCTURES USING STACK
1. Muthu has created a dictionary containing names and marks of computer science as key, value
pairs of 5 students. Write a program, with separate user defined functions to perform the
following operations:
● Push the keys (name of the student) of the dictionary into a stack, where the corresponding
value (CS marks) are more than or equal to 90.
● Pop and display the content of the stack, if the stack is empty display the message as “UNDER
FLOW” For example: If the sample content of the dictionary is as follows:
CS={"Raj":80, "Anu":91, "Vishwa":95, "Moni":80, “Govind":90}
The push operations output from the program should be:
Anu Vishwa Govind
The pop operation must display
Govind
Vishwa
Anu
“UNDER FLOW”
Ans:
CS={"Raj":80, "Anu":91, "Vishwa":95, "Moni":80, “Govind":90}
stk=[]
def PUSH():
for k,v in CS.items():
if v>=90:
stk.append(k)
def POP():
while len(stk):
print(stk.pop())
print(‘UNDERFLOW’)
PUSH()
POP()
2. Jiya has a list containing 8 integers. You need to help her create a program with two user defined
functions to perform the following operations based on this list.
• Traverse the content of the list and push those numbers into a stack which are divisible by both 5
and 3.
• Pop and display the content of the stack.
For example: If the sample Content of the list is as follows:
L=[5,15,21,30,45,50,60,75]
Sample Output of the code should be: 75 60 45 30 15
Ans:
L=[5,15,21,30,45,50,60,75]
stk=[]
def PUSH():
for x in L:
if x%3==0 and x%5==0:
stk.append(x)
def POP():
while stk:
print(stack.pop())
print(‘Stack empty’)
PUSH()
POP()
3. 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) (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]]
Ans:
def push_element(NList):
for i in NList:
if i[2]<3500 and i[1]!="India" :
travel.append([i[0],i[1]])
def pop_element():
while len(travel)!=0:
print(travel.pop())
else:
print('stack is empty')
travel=[]
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
push_element(NList)
pop_element()
4.Which list method can be used to perform Push operation in a stack implemented
by list?
(a) append()
(b) extend()
(c) push()
(d) insert()
Ans:
a)append()
5. Write a function in Python PUSH(Arr),where Arr is a list of numbers, from this list push all even
numbers into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display “stack empty” message.
Ans:
stk=[]
def push(Arr):
for x in Arr:
if x %2 ==0:
stk.append(x)
def pop():
while len(stk0)!=0:
print(stk.pop())
print(‘stack is empty’)
6. Write a function in Python PUSH(mydict),where mydict is a dictionary of phone book(name and
mobile numbers), from this dictionary push only phone numbers having 10 digits into a stack
implemented by using list .
Display the stack if it has at least one element, otherwise display “stack empty” message.
>>>mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:"Karun",674123789:"Tharun"
}
>>> push(mydict)
Digit of phone number 8 Phone number: 94567892 doesn't have 10 digits, which can't be pushed
Digit of phone number 13 Phone number: 8657456789012 doesn't have 10 digits, which can't be
pushed Stack elements after push operation : [9674123789, 1234567890]
Ans:
mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:"Karun",674123789:"Tharun"}
stk=[]
def PUSH():
for k,v in mydict.items():
if len(str(k))==10:
stk.append(k)
def POP():
while len(stk)!=0:
print(stk.pop())
print(‘stack is empty’)
PUSH()
POP()
7. Consider the following operation performed on a stack of size 3, What will be the output? (* top
position)
Push(10)
Push(20)
Push(30)
Pop()
Push(40)
Push(50)
(a) overflow
(b) underflow
(c) 10 20 30 40 50*
(d) 10 20 40 50*
Ans:
(d) 10 20 40 50*
8. Write a function in Python, Push(SItem) where , SItem is a dictionary
containing the details of stationary items– {Sname:price}.
The function should push the names of those items in the stack who have price
greater than 75. Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Sitem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be:
The count of elements in the stack is 2
Ans:
stackItem=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=75):
stackItem.append(k)
count=count+1
print("The count of elements in the stack is : ",count)
SItem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
Push(SItem)
9. Write a python function to:
1. Insert an element at the top of the stack
2. Delete an element at the top of the stack
Ans:
def insert():
item=int(input(‘Add element to stack’))
Stack.append(item)
def delete():
if len(stack)!=-0:
print(stack.pop())
else:
print(‘UNDERFLOW’)
10. What is LIFO data structure? Give any two applications of a stack?
LIFO: It stands for Last-In-First-Out approach in programming.
The data structure that implements LIFO is Stack.
The insertion of elements is known as PUSH operation and the removal of elements is known
as POP operation. Both operations take place through only one end of the stack called top of
the stack
Applications of a stack:
Reversing word
"undo" mechanism in text editors
Finding factorials