-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort stack.py
More file actions
43 lines (37 loc) · 894 Bytes
/
Copy pathsort stack.py
File metadata and controls
43 lines (37 loc) · 894 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
class stack:
def __init__(self):
self.s=[]
def push(self, val):
if len(self.s)==0:
self.s.append(val)
else:
temp=[]
while len(self.s)>0 and val>self.s[-1]:
temp.append(self.s.pop(-1))
self.s.append(val)
while len(temp)!=0:
self.s.append(temp.pop(-1))
def pop(self):
return self.s.pop(-1)
def peek(self):
return self.s[-1]
def isEmpty(self):
return len(self.s)==0
def display(self):
print('Top->', end="")
for i in self.s[::-1]:
print(str(i)+'->', end="")
print('|')
if __name__ == '__main__':
A=stack()
A.push(4)
A.push(2)
A.push(1)
A.display()
A.push(8)
A.display()
print(A.pop())
A.display()
print(A.peek())
A.display()
print(A.isEmpty())