-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
executable file
·55 lines (38 loc) · 1.33 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Stack:
'''A simple, generic stack data structure.'''
class StackNode:
def __init__(self, item, next_node):
self.item = item
self.next = next_node
class EmptyStackException(Exception):
pass
def __init__(self):
'''Initializes self.'''
self.top = None
self.count = 0
def __len__(self):
'''Returns len(self).'''
return self.count
def push(self, item):
'''push(item) -> None -- Pushes item to the top.'''
t = self.StackNode(item, self.top)
self.top = t
self.count += 1
def pop(self):
'''pop() -> item -- removes and returns the item at the top.
Raises EmptyStackException if the stack is empty.'''
if not self.top:
raise self.EmptyStackException('stack is empty')
item = self.top.item
self.top = self.top.next
self.count -= 1
return item
def peek(self):
'''peek() -> item -- returns (without removing) the item at the top.
Raises EmptyStackException if the stack is empty.'''
if not self.top:
raise self.EmptyStackException('stack is empty')
return self.top.item
def is_empty(self):
'''is_empty() -> boolean -- asserts if the stack is empty.'''
return not self.top