forked from iabhigupta/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
44 lines (40 loc) · 802 Bytes
/
stack.py
File metadata and controls
44 lines (40 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Stack:
'''Stack class'''
def __init__(self,size):
self.stack=[]
self.size=size
def push(self,elem):
try:
if len(self.stack)==self.size:
raise Exception
except Exception as e:
print("Stack full\n")
else:
self.stack.append(elem)
def pop(self):
try:
x=self.stack.pop()
except:
print("Empty stack\n")
else:
return x
def display(self):
print("\nThe stack contents are:")
for i in self.stack:
print(i,end=' ')
print("\n")
n=int(input("Enter the size of the stack"))
obj=Stack(n)
while True:
ch=int(input("1-Push\n2-Pop\n3-Display\n4-Exit\t"))
if ch==1:
e=int(input("\nEnter the element\t"))
obj.push(e)
elif ch==2:
e=obj.pop()
if e!=None:
print("The returned object is:{}\n".format(e))
elif ch==3:
obj.display()
else:
break