-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
38 lines (33 loc) · 936 Bytes
/
stack.py
File metadata and controls
38 lines (33 loc) · 936 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
36
37
38
class Level(object):
"""
Level class to be used by Stack class
Create a Level to reference another Level in a Stack
"""
def __init__(self, data, under=None):
self.data = data
self.under = under
class Stack(object):
"""
Stack class using the Level class for individual node
Create an empty Stack
"""
def __init__(self):
self.top = None
self.size = 0
"""
Push top Level down then add a Level on top
"""
def push(self, val):
self.top = Level(val, self.top)
self.size += 1
"""
Pop the top and return Level and the Level under is now the top
Raise IndexError if there is no top Level because Stack is empty
"""
def pop(self):
if self.size == 0:
raise IndexError("Can't pop empty Stack")
result = self.top
self.top = self.top.under
self.size -= 1
return result