-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
109 lines (84 loc) · 2.72 KB
/
stack.py
File metadata and controls
109 lines (84 loc) · 2.72 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from typing import Any, Generic, TypeVar
from python.linked_list.node import SinglyNode
from python.utils.errors import StackUnderflowError
T = TypeVar('T')
class ArrayStack(Generic[T]):
def __init__(self):
self.stack: list[T] = []
def push(self, el: T):
self.stack.append(el)
def pop(self):
if not self.stack:
raise StackUnderflowError()
return self.stack.pop()
def peek(self):
if not self.stack:
raise StackUnderflowError()
return self.stack[-1]
def empty(self):
"""Check if the stack is empty.
:return: True if stack is empty, else False
"""
return not self.stack
class LinkedListStack:
def __init__(self):
"""Initialize stack with stack pointer"""
self.top = None
self.size = 0
def __iter__(self):
curr = self.top
while curr:
val = curr.val
curr = curr.next
yield val
def __str__(self) -> str:
return '[]' if self.empty() else f'[{"->".join([str(node) for node in self])}]'
def push(self, key, value: Any) -> None:
"""Add node to the top of the stack"""
node = SinglyNode(key, value)
if self.top:
node.next = self.top
self.top = node
self.size += 1
def pop(self):
"""
Remove the top element from the stack and return it, or return
None if the stack is empty
"""
if not self.top:
raise StackUnderflowError()
node = self.top.val
self.size -= 1
self.top = self.top.next if self.top.next else None
return node
def peek(self):
"""Get the value of the top element from the stack.
:return: the top element from the stack, or None if the stack is empty
"""
if not self.top:
raise StackUnderflowError()
return self.top.val
def empty(self) -> bool:
"""Check if the stack is empty.
:return: True if stack is empty, else False
"""
return self.size == 0
if __name__ == '__main__':
arr_stack: ArrayStack[Any] = ArrayStack()
print(arr_stack.empty()) # True
ll_stack = LinkedListStack()
print('Checking if stack is empty:', ll_stack.empty()) # True
ll_stack.push('1', 1)
ll_stack.push('2', 2)
print(ll_stack) # [2->1]
ll_stack.push('3', 3)
ll_stack.push('4', 4)
print('Checking item on top of stack:', ll_stack.peek()) # 4
ll_stack.push('5', 5)
print(ll_stack) # [5->4->3->2->1]
print(ll_stack.pop()) # 5
print(ll_stack.pop()) # 4
print(ll_stack) # [3->2->1]
ll_stack.push('6', 4)
print(ll_stack) # [4->3->2->1]
print(ll_stack.peek()) # 4