-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_linked_list.py
67 lines (56 loc) · 1.42 KB
/
stack_linked_list.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
63
64
65
66
67
class Node:
def __init__(self, data: any) -> None:
self.data = data
self.next = None
def __str__(self) -> str:
return str(self.data)
class Stack:
def __init__(self) -> None:
self.top = None
def __len__(self) -> None:
size = 0
run = self.top
while run:
size += 1
run = run.next
return size
def __iter__(self) -> None:
run = self.top
while run:
yield run
run = run.next
def __str__(self) -> str:
if self.is_empty():
return "Stack is Empty"
else:
return '->'.join([str(node) for node in self])
def is_empty(self) -> bool:
return not bool(self.top)
def push(self, data: any) -> None:
node = Node(data)
if self.is_empty():
self.top = node
else:
node.next = self.top
self.top = node
def pop(self) -> any:
if not self.is_empty():
ret = self.top
self.top = ret.next
return ret.data
def peek(self) -> any:
if not self.is_empty():
return self.top.data
def main():
pass
# s = Stack()
# s.push(1)
# s.push(2)
# s.push(3)
# print(s.pop())
# print(s)
# print(s.peek())
# print(len(s))
# print(s.is_empty())
if __name__ == '__main__':
main()