-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
60 lines (47 loc) · 1.49 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
from LinearData import LinearData
class Stack(LinearData):
'''Last In First Out data structure where pops and pushes only happen at one end.'''
def __init__(self, *args, unpack=True):
super().__init__(*args, unpack=True)
self.delimiter = ' / '
def append(self, item):
'''In a Stack, items are appended to the top / left'''
super().append(item, right=False)
def pop(self):
'''In a Stack, items are popped from the top / left'''
return super().pop(right=False)
if __name__ == "__main__":
print("Testing empty stack")
tst_stack = Stack()
print(tst_stack)
print("Pushing some items")
tst_stack.append(1)
print(tst_stack)
tst_stack.append(2)
print(tst_stack)
tst_stack.append(3)
print(tst_stack)
print("Popping some items")
for i in range(3):
print(tst_stack.pop())
print("Pushing some items")
tst_stack.append(1)
print(tst_stack)
tst_stack.append(2)
print(tst_stack)
tst_stack.append(3)
print(tst_stack)
tst_stack.append(4)
print(tst_stack)
tst_stack.append(5)
print(tst_stack)
print("Testing contains...")
print(f"tst_stack.contains(1): {tst_stack.contains(1)}")
print(f"tst_stack.contains(5): {tst_stack.contains(5)}")
print("Testing reverse...")
print(f"Current stack:{tst_stack}")
print("...reversing...")
tst_stack.reverse()
print(f"Now: {tst_stack}")
new_stack = Stack([1, 2, 3, 4, 5, 6, 8])
print(new_stack)