-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.py
57 lines (50 loc) · 1.12 KB
/
stack.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
stack=[]
top = None
def isempty(stack):
if stack==[]:
return True
else:
return False
#Function to push elements into the stack
def push(stk):
item=eval(input("Enter the Name number price"))
stk.append(item)
top=len(stk)-1
#Function to pop elements from the stack
def pop(stk):
if isempty(stack):
print("Underflow")
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk) - 1
print("Popped Item = ", item)
#Function to display elements in the stack
def display(stk):
if isempty(stack):
print("Underflow")
else:
top=len(stk)-1
print(stk[top],"<-top")
for i in range (top-1,-1,-1):
print(stk[i])
#Main program - Menu
while True:
print("Stack Operations")
print("1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")
ch=int(input("Enter your Choice"))
if ch==1:
push(stack)
elif ch==2:
pop(stack)
elif ch==3:
display(stack)
elif ch==4:
break
else:
print("Invalid Choice")