-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.py
80 lines (75 loc) · 2.03 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#https://www.tutorialspoint.com/python/python_lists.htm
#https://www.tutorialspoint.com/python/python_basic_operators.htm
# Stack Operation
#Push Operation
def push(x,y,z):
x.append(y)
maxpush(y,z)
print("Stack after Push Operation:\n",x)
#Pushing data into Max value stack
def maxpush(x,y):
if(len(y)==0):
maxval=0
else:
maxval=y[len(y)-1]
if (int(x) >= int(maxval)):
maxval=x
y.append(x)
#Pop Operation
def pop(z,m):
c = len(z)
if (c == 0):
print("Stack is empty.\n")
else:
top_v =z[c-1]
z.pop()
print("Stack after Pop Operation:\n", z)
temp = m[len(m)-1]
if( int(top_v) == int(temp)):
m.pop()
#Topmost element
def top(z):
c=len(z)
if(c==0):
print("Stack is empty.\n")
else:
print("Top most element of stack:\n",z[c-1])
'''
#Find Maximum value - Alternative method
def maxval(z):
c=len(z)
if (c==0):
print("Stack is empty.\n")
else:
maxv = z[0]
for i in range(1,c):
if z[i] > maxv:
maxv = z[i]
print("Max value in the stack is:\n", maxv)
'''
#Using List datatype for stack operation
stack = []
maxvalst =[]
operation =(1,2,3,4,5)
var = 1
while var == 1 : # This constructs an infinite loop
num = int(input("Enter a number to perform Stack Operation:\n1.Push\n2.Pop\n3.Top Element\n4.Max Value in Stack\n5.Exit\n"))
if(num in operation):
if(num==1):
zz=input("Enter the value to push into stack:")
push(stack,zz,maxvalst)
print("Max Value Stack:\n", maxvalst)
elif(num==2):
pop(stack,maxvalst)
print("Max Value Stack:\n", maxvalst)
elif(num==3):
top(stack)
elif(num==4):
if (len(maxvalst)==0):
print("Stack is empty.\n")
else:
print("Max Value in the stack is:\n",maxvalst[len(maxvalst)-1])
else:
break
else:
print("Enter a valid Stack operation.\n")