STACK IMPLEMENTATION
def push(stk,item):
stk.append(item)
print("%d is succesfully added"%item)
def s_pop(stk):
if stk==[]:
print("stack is empty")
else:
i=stk.pop()
return i
def display(stk):
a=stk[::-1]
print(a)
stk=[]
print("\t\t\t STACK IMPLEMENTATION")
print("\t\t\t *************************")
while True:
print(" 1.Push\n 2.Pop\n 3.Display\n 4.Quit")
ch=int(input("Enter your choice:"))
if ch==1:
no=int(input("Enter the number to be added:"))
push(stk,no)
elif ch==2:
item=s_pop(stk)
print("%d is popped"%item)
elif ch==3:
display(stk)
elif ch==4:
break
else:
print("Invalid operation!!!")
OUTPUT:
STACK IMPLEMENTATION
*************************
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the number to be added:4
4 is succesfully added
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the number to be added:7
7 is succesfully added
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the number to be added:2
2 is succesfully added
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the number to be added:8
8 is succesfully added
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:1
Enter the number to be added:3
3 is succesfully added
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
[3, 8, 2, 7, 4]
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
3 is popped
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:23
Invalid operation!!!
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
[8, 2, 7, 4]
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
8 is popped
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
2 is popped
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
7 is popped
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
[4]
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:2
4 is popped
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:3
[]
1.Push
2.Pop
3.Display
4.Quit
Enter your choice:4