Stack Programs
Stack Programs
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:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]
The stack should contain [“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
The output should be: [“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty Ans:
status=[]
def Push_element(cust):
if cust[2]=="Goa":
L1=[cust[0],cust[1]]
status.append(L1)
def Pop_element ():
num=len(status)
while num!=0:
dele=status.pop()
print(dele)
num=num-1
else:
print("Stack Empty")
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)
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
for k in EMP:
if EMP[k] < 25000:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
4) Aryan has a list containing 10 integers. You need to help him create a program with separate
user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the odd numbers into a stack.
● Pop and display the content of the stack.
For Example: If the sample Content of the list is as follows:
Num=[31, 55, 76, 89, 21, 45, 76, 68 ] Sample Output of the code should be: 45 21 89 55 31
Num=[31, 55, 76, 89, 21, 45, 76, 68 ]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
for k in N:
if k%2!=0:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
5) Julie has created a dictionary containing names and marks as key value pairs of 6 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 (marks) is greater than 75.
● Pop and display the content of the stack. For example: If the sample content of the dictionary is
as follows
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be: TOM ANU BOB OM
6. Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack. For Example: If the sample Content of the list is as
follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
S. append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[ ]:
print(POP(ST),end=" ")
else:
break
7) Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all
numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.
OR
Write a function in Python POP(Arr), where Arr is a stack implemented by a list of numbers. The
function returns the value deleted from the stack.
def PUSH(Arr,value):
s=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
OR
def popStack(st) :
# If stack is empty
if len(st)==0:
print("Underflow")
else:
L = len(st)
val=st[L-1]
print(val)
8.Vedika has created a dictionary containing names and marks 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 (marks) is greater than 70.
Pop and display the content of the stack.
def push(stk,item):
stk.append(item)
def Pop(stk):
if stk==[]:
return None
else:
return stk.pop()
stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}
for i in d:
if d[i]>70:
push(stk,i)
while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
def pop(Lst):
if(Lst==[]):
print("No Record")
else:
print("Deleted Record is :",Lst.pop())
def display(Lst):
l=len(Lst)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(Lst[i][0],"\t\t",Lst[i][1],"\t\t",Lst[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice"))
if(op==1):
push(Lst)
elif(op==2):
pop(Lst)
elif(op==3):
display(Lst)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N)")