forked from jwarren116/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
30 lines (25 loc) · 891 Bytes
/
stack.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
class StackItem(object):
def __init__(self, data, next_item=None):
self.data = data
self.next_item = next_item
def __str__(self):
return str(self.data)
class Stack(object):
def __init__(self, first_item=None):
self.first_item = first_item
def push(self, val):
# push val to beginning of list
self.new_item = StackItem(val)
if not self.first_item:
self.first_item = self.new_item
else:
self.new_item.next_item = self.first_item
self.first_item = self.new_item
def pop(self):
# poops first value from list and returns it
if self.first_item is None:
raise ValueError("No items in stack!")
else:
obsolete_item = self.first_item
self.first_item = self.first_item.next_item
return obsolete_item.data