Programs On Stack
Programs On Stack
top=-1
stk=[]
for i in L:
if i%2==0:
stk.append(i)
top=len(stk)-1
2.Write a function in Python POP_OUT(Stk), where Stk is a stack implemented by a list of numbers. The
function returns the value which is deleted/popped from the stack.
if stk==[]:
return True
else:
return False
def POP_OUT(stk):
print("Stack Underflow")
item=stk.pop()
if len(stk)==0:
top=-1
else:
top=len(stk)
return item
3. 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.
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:
4. 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.
if len(st)==0:
print("Underflow")
else:
L = len(st)
val=st[L-1]
print(val)
5. 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
● Push the keys (Employee code) of the dictionary into a stack, where the corresponding value
For example:
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=" ")
else:
break
6.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.
For Example:
45 21 89 31
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.Ashish students of class XII wants to enter details of student’s- Rollno, Name and grade in a stack.
Help him to write Push() methods in Python to add student’s details. Display the student’s details.
def push(stack):
s=[]
stack.append(s)
l=len(stack)
for i in range(l-1,-1,-1):
print stack[i]
stack=[]
student = []
display(stack)
stk=[]
top=-1
def POP():
if(top==-1):
else:
top=len(stk)-1
def display():
if(top==-1):
else:
t=len(stk)-1
print(stk[t])
for i in range(t-1,-1,-1):
print(stk[i])
display()
POP()
9. Write a function in python, MakePush(Package) and MakePop(Package) to add a new Package and
delete a Package from a List of Package Description, considering them to act as push and pop operations
o def MakePush(Package):
Package.append(a)
def MakePop(Package):
if (Package==[]):
else:
10. Write a function in python, Push(Stu) and MakePop(Stu) to add a new student and delete student
from a List of Stu contain rollno, Sname and Class as list, considering them to act as push and pop
operations of the Stack data structure
def Push(Stu):
info=[rollno,Sname,Class]
Stu.append(info)
def Pop(Stu):
if (Stu==[]):
11. 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
def Push(Package):
Package.append(T)
def Pop(Package):
if (Package==[]):
else:
12. Write a user define function to push an item of integer type into stack (function to push information
of student include rollno and name in the form of list/tuple or dictionary.)
stack=[ ]
stack.append(item)
stack=[ ]
stack.append(item)
13 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.
stk=[ ]
def push(stk):
stk.append(student_name)
def pop(stk):
if(stk==[]):
print("Stack is empty")
else: