Professional Documents
Culture Documents
Write a Python Program to Declare a Stack list and display the following menu:
Push
use append() function in list
Pop
us pop() in list
Display
Exit
L=[]
def Push(L,e):
L.append(e)
def IsEmpty(L):
if L==[]:
return True
else:
return False
def Pop(L):
if IsEmpty(L):
return "underflow"
else:
e=L.pop()
return e
def Display(L):
if IsEmpty(L):
print( "underflow")
else:
print("Stack:")
for a in range(len(L)-1,-1,-1):
print(L[a])
while True:
print("""
1. Push
2. Pop
3. Display
4. Exit""")
ch=int(input("Enter your choice:"))
if ch==1:
e=int(input("Enter data to push:"))
Push(L,e)
elif ch==2:
e=Pop(L)
if e=="underflow":
print("Stack is empty")
else:
print(e," is popped")
elif ch==3:
Display(L)
elif ch==4:
break
else:
print("Wrong choice")
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:1
Enter data to push:12
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:1
Enter data to push:13
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:1
Enter data to push:14
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:3
Stack:
14
13
12
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:2
14 is popped
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:3
Stack:
13
12
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:4
Q2. Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Disp
L=[]
def Push(L,e):
if e%2==1:
L.append(e)
def IsEmpty(L):
if L==[]:
return True
else:
return False
def Pop(L):
if IsEmpty(L):
return "underflow"
else:
print("Stack:")
Max=L[0]
while L!=[]:
e=L.pop()
if e>Max:
Max=e
print(e)
print("Largest odd is ",Max)
Q3. Vedika has created a dictionary containing names and marks as key-value pairs of 5 students. Write a program, w
Push the keys (name of the student) of the dictionary into a stack, where the corresponding value (marks) is greate
Pop and display the content of the stack.
The dictionary should be as follows:
L=[]
def Push(L,D):
for K,V in D.items():
if V>=70:
L.append(K)
def IsEmpty(L):
if L==[]:
return True
else:
return False
def Pop(L):
if IsEmpty(L):
return "underflow"
else:
print("Students getting above 70:")
Max=L[0]
while L!=[]:
e=L.pop()
print(e)