-
Notifications
You must be signed in to change notification settings - Fork 22
/
3.1.py
44 lines (35 loc) · 1.23 KB
/
3.1.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
# Use a single array to implement 3 stacks
class ThreeStacks(object):
def __init__(self, size_of_stack):
self.stack_list = [ None for _ in range(3 * size_of_stack) ]
self.p = [0, 100, 200]
def push(self, stack_index, data):
if self.p[stack_index] == 100 * stack_index + 100:
raise Exception("Stack is full.")
else:
self.stack_list[self.p[stack_index]] = data
self.p[stack_index] += 1
def pop(self, stack_index):
if self.is_empty(stack_index):
raise Exception("Stack is empty.")
else:
data = self.stack_list[self.p[stack_index]]
self.stack_list[self.p[stack_index]] = None
self.p[stack_index] -= 1
return data
def peek(self, stack_index):
if self.is_empty(stack_index):
raise Exception("Stack is empty.")
else:
return self.stack_list[self.p[stack_index]]
def is_empty(self, stack_index):
return self.p[stack_index] == 100 * stack_index
if __name__ == "__main__":
s = ThreeStacks(100)
# s.peek(0)
for i in range(100):
s.push(0, i)
for i in range(100):
print(s.pop(0))
print(s.is_empty(0))
s.pop(1)