VELAMMAL VIDHYASHRAM, SURAPET
WORKSHEET -DATA STRUCTURES USING STACK
1.A list contains following record of a doctor:
[Doc_ID, Doc_name, Phone_number, Speciality]
Write the following user defined functions to perform given operations on the stack named "status":
(i) Push_element() - To Push an object containing Doc_ID and Doc_nameof doctors who
specialize in Anesthesia to the stack.
(ii) (ii) Pop_element() - To Pop the objects from the stack and display them. Also, display
―Stack Empty when there are no elements in the stack.
Ans:
def push_element(NList):
for i in NList:
if i[-1]==’Anesthesia’:
status.append([i[0],i[1]])
def pop_element():
while len(status)!=0:
print(status.pop())
print('stack is empty')
status=[]
Nlist=[[‘D01’,’GURU’,’9999988888’,’Anesthesia’], [‘D02’,’VARUN’,’9899988889’,’Cardiologist],
[‘D03’,’NEHA’,’979988878’,’Anesthesia’], [‘D04,’BEN’,’9349988834’’,’Medicine’]]]
push_element(NList)
pop_element()
2.Write a function in Python, Push(KItem), where KItem is a dictionary containing 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 the stack.
For 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
Kitem={"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
stk=[]
def Push(Kitem):
sum=0
for k,v in Kitem.items():
if v<100:
stk.append(k)
sum=sum+v
print(‘The average price of an item is’,sum/len(stk))
Push(Kitem)
3.A list contains following record of a customer:
[Customer_name,Phone_number,City]
Write the following user defined functions to perform given operations on the stack named ‘status’:
i)Push_element() – To Push an object containing name and Phone number of customers who live in
Goa to the stack
ii)Pop_element() – To Pop the objects from the stack and display them. Also, display ‘Stack Empty’
when there are no elements in the stack
For example:
If the lists of customer details are:
[‘Ashok’,’9999988888’ ,’Goa’]
[‘Avinash’, ‘8888899999’,’Mubai’]
[‘Mahesh’,’7777777777’,’Cochin’]
[‘Rakesh’, ‘6666666666’,’Goa’]
The stack should contain:
[‘Rakesh’,’666666666’]
[‘Ashok’,’ 9999988888’]
The output should be:
[‘Rakesh’,’666666666’]
[‘Ashok’,’ 9999988888’]
Stack Empty
Ans:
status=[]
cust=[[‘Ashok’,’9999988888’ ,’Goa’], [‘Avinash’, ‘8888899999’,’Mubai’]
[‘Mahesh’,’7777777777’,’Cochin’] ,[‘Rakesh’, ‘6666666666’,’Goa’]]
def push_element(cust):
for i in cust:
if i[2]==’Goa’:
status.append([i[0],i[1]])
def pop_element():
while len(status)!=0:
print(status.pop())
print('stack is empty')
push_element(cust)
pop_element()
4.Write a program in python to reverse a string using stack.
Ans:
def Reverse(Original):
stack=[]
r=’’
for x in original:
stack.append(x)
for x in range(len(stack)):
r=r+stack.pop()
print(“Reveresed string “,r)
orginal=input(“Enter any string:”)
Reverse(original)
5.Write down the status of stack after each operation
Stack=[10,20,30,40]
Where TOP item is 40
i)push 70
ii)Push 100
iii)Pop an item from stack
iv)Peek the stack
Ans:
i) [10,20,30,40,70]
ii) [10,20,30,40,70,100
iii)] [10,20,30,40,70]
iv)70
6. Write a function in python, Push(Package) and Pop(Package) to add details of employee contain
information (Empid, Ename and Salary) in the form of tuple in Package and delete a Package from a
List of Package Description, considering them to act as push and pop operations of the Stack data
structure.
Ans:
Package=[]
def Push(Package):
while True:
Empid=int(input(“Enter empid”))
Ename=input(“Enter Emp name”))
Salary=int(input(“Enter Salary”))
Ch=input(“Do you want to add records”)
if Ch in ‘Nn’:
break
Package.append((Empid,Ename,Salary))
def Pop(Package):
while len(Package)!=0:
print(Package.pop())
print(“Stack is empty”)
7. Choose the correct output for the following stack operation(* top position)
Push(5)
Push(8)
Pop()
Push(2)
Push(5)
Pop()
Push(1)
a) 8 5 2 5 1*
b) 8 5 5 2 1*
c) 2551*
d) 521*
Ans: 5 2 1*
8. Consider STACK=[‘a’,’b’,’c’,’d’]. Write the STACK content after each operations:
a) STACK.pop( )
b) STACK.append(‘e’)
c) STACK.append(‘f’)
d) STACK.pop( )
Ans:
['a', 'b', 'c']
['a', 'b', 'c',’e’]
['a', 'b', 'c',’e’,’f’]
['a', 'b', 'c',’e’]
9. 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
last digit is greater than 5 to a stack implemented by using list and also display the stack if
it has at least one element, otherwise display “stack empty” message.
For example,
mydict={9446789123:"Ram",8889912345:"Sam",7789012367:"Sree"}
Phone number: 9446789123 last digit is less than five which can't be pushed Stack
elements after push operation : [7789012367, 8889912345]
Ans:
Mydict={9898456123:'tina',9090909097:'Sri'}
stk=[]
def push():
for k,v in Mydict.items():
if k%10>5:
stk.append(k)
def pop():
while len(stk)!=0:
print(stk.pop())
print(‘Stack is empty’)
push()
pop()
10. A line of text is read from the input terminal into a stack. Write a program to output the string
in reverse order, each character appearing twice. (e.g., the string a b c d e should be changed to ee
dd cc bb aa)
Ans:
stack=[]
line=input('Enter a line of text')
def push():
for x in line:
stack.append(x)
def pop():
while len(stack)!=0:
print(stack.pop()*2,end=' ')
push()
pop()