Stack Implementation using Python
Code is given below:
#Stack
class stack:
def __init__(self):
self.stack=[]
def push(self,value):
if value not in self.stack:
self.stack.append(value)
else:
print("Data is already there in stack...")
def pop(self):
if self.stack is None:
print("Stack is empty")
else:
self.stack.pop()
print("Popped Successfully")
def display(self):
for i in range(len(self.stack)-1,-1,-1):
print(self.stack[i])
obj=stack()
print("STACK OPERATIONS")
while True:
print("\nPress\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT")
choice=int(input("Enter your choice : "))
if choice==1:
number=int(input("Enter the number : "))
obj.push(number)
elif choice==2:
obj.pop()
elif choice==3:
obj.display()
elif choice==4:
break
else:
pass
Comments
Post a Comment