-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_linked.py
62 lines (47 loc) · 1.32 KB
/
stack_linked.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
class Stack:
def __init__(self):
self.top = None
self.size = 0
def push(self,value):
self.size += 1
new_top = Node(value)
new_top.set_next(self.top)
self.top = new_top
def pop(self):
if self.is_empty():
raise EmptyStack("Can't pop empty stack.")
self.size -= 1
return_value = self.top.get_value()
self.top = self.top.get_next()
return return_value
def peek(self):
if self.is_empty():
raise EmptyStack("Can't peek empty stack.")
return self.top.get_value()
def is_empty(self):
return self.top is None
def __len__(self):
return self.size
class Node:
def __init__(self,value):
self.value = value
self.next = None
def get_value(self):
return self.value
def set_value(self,value):
self.value = value
def get_next(self):
return self.next
def set_next(self,new_next):
self.next = new_next
class EmptyStack(Exception):
pass
if __name__ == "__main__":
my_stack = Stack()
for code_point in range(ord('a'),ord('z')+1):
my_stack.push(chr(code_point))
while not my_stack.is_empty():
print(len(my_stack))
print(my_stack.pop())
print(len(my_stack))
print()