0% found this document useful (0 votes)
35 views

Stack Programs

St

Uploaded by

akashhar56
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Stack Programs

St

Uploaded by

akashhar56
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

STACK PROGRAMS

1) 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:
[“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")

2) Write a function in Python, Push(SItem) where , SItem is a dictionary containing


the details of stationary items– {Sname:price}. 2023(SQP)
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:
Ditem={"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

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)

3) Pramod has created a dictionary containing EMPCODE and SALARY as key


value pairs of 5 Employees of Parthivi Constructions. Write a program, with
separate user defined functions to perform the following operations:
● Push the keys (Employee code) of the dictionary into a stack, where the
corresponding value (Salary) is less than 25000.
● Pop and display the content of the stack. For example: If the sample content of
the dictionary is as follows:

EMP={"EOP1":16000, "EOP2":28000, "EOP3":19000, "EOP4":15000, "EOP5":30000}


The output from the program should be: EOP4 EOP3 EOP1 O

EMP={"EOP1":16000, "EOP2":28000, "EOP3":19000, "EOP4":15000,


"EOP5":30000}

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

R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}


def PUSH(S,N):
S. append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[ ]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[ ]:
print(POP(ST),end=" ")
else:
break

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.

The dictionary should be as follows:


d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
Then the output will be: Umesh Vishal Ishika

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

Q9. Write a function push(student) and pop(student) to add


a new student name and remove a student name from a list
student, considering them to act as PUSH and POP
operations of stack Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter name of student")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())

Q10. Write a function push(number) and pop(number) to add


a number (Accepted from the user) and remove a number
from a list of numbers, considering them act as PUSH and
POP operations of Data Structure in Python.
st=[ ]
def push(st):
sn=input("Enter any Number")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted Number is :",st.pop())
Q11. Write a menu based program to add, delete and display
the record of hostel using list as stack data structure in
python. Record of hostel contains the fields : Hostel number,
Total Students and Total Rooms.
Lst=[ ]
ch='y'
def push(Lst):
hn=int(input("Enter hostel number"))
ts=int(input("Enter Total students"))
tr=int(input("Enter total rooms"))
temp=[hn,ts,tr]
Lst.append(temp)

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)")

You might also like