forked from AllAlgorithms/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
29 lines (24 loc) · 648 Bytes
/
Copy pathStack.py
File metadata and controls
29 lines (24 loc) · 648 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
class Node:
def __init__(self, value):
self.value = value
self.next = None
class Stack:
def __init__(self):
self._first = None
self._size = 0
def __len__(self):
return self._size
def push(self, item):
new_node = Node(item)
if self._first is None:
self._first = new_node
else:
new_node.next = self._first
self._size += 1
def pop(self):
if self._first is None:
raise IndexError
val = self._first.value
self.first = self.first.next
self._size -= 1
return val