In this post,we are going to write python program to implement stack for student-details using list data structure. In python,we can use list data structure to implement stack. Python offers us a convenient set of methods to operate lists as stacks. For various stack operation,we can use a list say stack that may contain logically group information such as student details like : roll number,student name,age etc and then list will be entered as an item to the stack
Write a python program to implement a stack for student (roll number,student name)and add a new student and remove student from list of students,considering them act as PUSH and POP operations of data structure.
In this python program ,we will write add(student) and remove(student)method to add new student in class and remove student from list of student. We will also implement display operations on stack. Each item node of stack will contain two types of information-a roll number and student name.
lets write a python program
Python program to implement a stack for student-details
def isEmpty(stk):
if stk == []:
return True
else:
return False
def add(stk,item):
stk.append(item)
top = len(stk)-1
def remove(stk):
if(stk==[]):
print("Stack empty;UNderflow")
else:
print("Deleted student is :",stk.pop())
def display(stk):
if isEmpty(stk):
print("Stack empty ")
else :
top = len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
stack=[]
top = None
while True:
print("STACK OPERATION:")
print("1.ADD student")
print("2.Display stack")
print("3.Remove student")
print("4.Exit")
ch = int(input("Enter your choice(1-4):"))
if ch==1:
rno = int(input("Enter Roll no to be inserted :"))
sname = input("Enter Student name to be inserted :")
item = [rno,sname]
add(stack,item)
input()
elif ch==2:
display(stack)
input()
elif ch==3:
remove(stack)
input()
elif ch==4:
break
else:
print("Invalid choice ")
input()
Output:
STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):1 Enter Roll no to be inserted :11 Enter Student name to be inserted :ATHANG STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):1 Enter Roll no to be inserted :12 Enter Student name to be inserted :SUJATA STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):1 Enter Roll no to be inserted :13 Enter Student name to be inserted :MEENA STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):1 Enter Roll no to be inserted :14 Enter Student name to be inserted :SUSHIL STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):1 Enter Roll no to be inserted :15 Enter Student name to be inserted :SUMEDH STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):2 [15, 'SUMEDH'] <-top [14, 'SUSHIL'] [13, 'MEENA'] [12, 'SUJATA'] [11, 'ATHANG'] STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):3 Deleted student is : [15, 'SUMEDH'] STACK OPERATION: 1.ADD student 2.Display stack 3.Remove student 4.Exit Enter your choice(1-4):4