-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_stack.py
More file actions
65 lines (52 loc) · 1.64 KB
/
Copy pathtwo_stack.py
File metadata and controls
65 lines (52 loc) · 1.64 KB
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
class StackError(Exception):
pass
class Stack:
def __init__(self, max_len: int):
self.max_len = max_len
self.top0 = -1
self.top1 = max_len
self.stack = ["None"] * max_len
def empty(self) -> bool:
return self.top0 == -1 and self.top1 == self.max_len
def full(self) -> bool:
return self.top0 + 1 >= self.top1
def __repr__(self):
return str(self.stack)
def stack_push(d_stack: Stack, element, flag: str = "1"):
if d_stack.full():
raise StackError("stack full")
if flag == "1":
d_stack.top0 += 1
d_stack.stack[d_stack.top0] = element
else:
d_stack.top1 -= 1
d_stack.stack[d_stack.top1] = element
def stack_pop(d_stack: Stack, flag: str = "1"):
if d_stack.empty():
raise StackError("stack empty")
if flag == "1":
if d_stack.top0 == -1:
raise StackError("stack_top0 empty")
else:
d_stack.stack[d_stack.top0] = "None"
d_stack.top0 -= 1
else:
if d_stack.top1 == d_stack.max_len:
raise StackError("stack_top1 empty")
else:
d_stack.stack[d_stack.top1] = "None"
d_stack.top1 += 1
if __name__ == '__main__':
stack_a = Stack(max_len=9)
stack_push(stack_a, 2)
stack_push(stack_a, 3)
stack_push(stack_a, 4)
stack_push(stack_a, 2, flag="2")
stack_push(stack_a, 3, flag="2")
stack_push(stack_a, 4, flag="2")
stack_push(stack_a, 5, flag="2")
stack_push(stack_a, 6, flag="2")
stack_push(stack_a, 7, flag="2")
stack_pop(stack_a, flag="1")
print(stack_a)
stack_push(stack_a, 8, flag="1")