-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
31 lines (24 loc) · 713 Bytes
/
stack.py
File metadata and controls
31 lines (24 loc) · 713 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
from data_structures.node import Node
class Stack:
def __init__(self):
self._top = None
def is_empty(self):
return self._top is None
def peek(self):
return (None if self.is_empty() else self._top.data)
def count(self):
if self.is_empty(): return 0
return self._top.count()
def push(self, data):
node = Node(data)
node.next = self._top
self._top = node
return data
def pop(self):
if self.is_empty(): return
temp = self._top
self._top = self._top.next
return temp.data
def to_array(self):
if self.is_empty(): return []
return self._top.to_array([])