-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
35 lines (22 loc) · 803 Bytes
/
Copy pathstack.py
File metadata and controls
35 lines (22 loc) · 803 Bytes
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
from data_structures.linked_list import Node
from data_structures.invalid_operation_error import InvalidOperationError
class Stack:
def __init__(self):
self.top = None
def push(self, value):
new_node = Node(value)
new_node.next = self.top
self.top = new_node
def pop(self):
if self.top is None:
raise InvalidOperationError("Method not allowed on empty collection")
pop_value = self.top.value
#move the pointer which "removes" the node
self.top = self.top.next
return pop_value
def peek(self):
if self.top is None:
raise InvalidOperationError("Method not allowed on empty collection")
return self.top.value
def is_empty(self):
return self.top is None