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

Programs On Stack

The document provides 13 questions related to implementing stack operations in Python using functions. The questions cover defining functions for PUSH and POP operations to add and remove elements from a stack implemented as a list. Elements that can be pushed and popped include numbers, strings, tuples, lists, and dictionaries. Functions are also defined to check if a stack is empty.

Uploaded by

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

Programs On Stack

The document provides 13 questions related to implementing stack operations in Python using functions. The questions cover defining functions for PUSH and POP operations to add and remove elements from a stack implemented as a list. Elements that can be pushed and popped include numbers, strings, tuples, lists, and dictionaries. Functions are also defined to check if a stack is empty.

Uploaded by

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

1.Write a function in Python PUSH_IN(L), where L is a list of numbers.

From this list, push all even


numbers into a stack which is implemented by using another list.

top=-1

stk=[]

def PUSH_IN(L): # Allow additions to the stack

for i in L:

if i%2==0:

stk.append(i)

top=len(stk)-1

( ½ marks for correct function header)

( 1 mark for correct accessing of list elements)

( ½ mark for correct condition for even number)

( ½ mark for applying append() correctly)

( ½ mark for assignment in variable top)

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.

def isEmpty(stk): # checks whether the stack is empty or not

if stk==[]:

return True

else:

return False

def POP_OUT(stk):

if isEmpty(stk): # verifies whether the stack is empty or not

print("Stack Underflow")

else: # Allow deletions from the stack

item=stk.pop()
if len(stk)==0:

top=-1

else:

top=len(stk)

return item

( ½ marks for correct POP_OUT() function header)

( ½ mark for checking empty stack status)

( ½ mark for removing item for stack )

( 1 mark for assignment in variable top)

( ½ mark for returning the deleted 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:

print(s) (3 M for correct code)

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.

def popStack(st) : # If stack is empty

if len(st)==0:

print("Underflow")
else:

L = len(st)

val=st[L-1]

print(val)

st.pop(L-1) (3 M for correct code)

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

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

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

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.

● 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 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

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=[]

s.append(input(“Enter student rollno?”))

s.append(raw_input(“Enter student name”))

s.append(raw_input(“Enter student grade”))

stack.append(s)

def display (stack):

l=len(stack)

print “STACK CONTENTS”

for i in range(l-1,-1,-1):

print stack[i]

stack=[]

print “Creating Stack”

n = input(“Enter the number of students”)


for i in range(n):

student = []

student.append(input(“Enter student rollno?”))

student.append(raw_input(“Enter student name”))

student.append(raw_input(“Enter student grade”))

stack, append(student) push(stack)

display(stack)

8.Write a program to implement a stack for the students(studentno, name). Just

implement Pop and display.

stk=[]

top=-1

def POP():

if(top==-1):

print(“NO STUDENT DATA”)

else:

print(“Student details are:”, stk.pop())

top=len(stk)-1

def display():

if(top==-1):

print(“NO STUDENT DATA”)

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

a=int(input("enter package title : "))

Package.append(a)

def MakePop(Package):

if (Package==[]):

print( "Stack empty")

else:

print ("Deleted element:",Package.pop())

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

rollno=int(input("enter package title : "))

Sname=int(input("enter package title : "))

Class=int(input("enter package title : "))

info=[rollno,Sname,Class]

Stu.append(info)

def Pop(Stu):

if (Stu==[]):

print( "Stack empty")


else:

print ("Deleted element:",Stu.pop())

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

Empid=int(input(“Enter Id of Employee: "))

Ename=input(“Enter Name of employee”)

Salary= int(input(“Enter Salary of an employee”))

T=(Empid, Ename ,Salary)

Package.append(T)

def Pop(Package):

if (Package==[]):

print( "Stack empty")

else:

print ("Deleted element:",Package.pop())

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

PUSH OPERATION ON STACK

// function to push an item of integer type into stack

stack=[ ]

def push (stack):

item=int(input(“Enter the values of item”))

stack.append(item)

// function to push information of student include rollno and name in the


form of list, tuple, dictionary

stack=[ ]

def push (stack):

rollno=int(input(“Enter rollno of student”))

name =input(“Enter Name of student”)

item=(rollno, name) \\ [rollno, name] \\ {rollno : “name”} \\ as per the problem

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

student_name=input("Enter name of student")

stk.append(student_name)

def pop(stk):

if(stk==[]):

print("Stack is empty")

else:

print("Deleted student name :",stk.pop())

You might also like