-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_stack.py
More file actions
40 lines (31 loc) · 1.05 KB
/
array_stack.py
File metadata and controls
40 lines (31 loc) · 1.05 KB
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
from empty import Empty
class ArrayStack:
"""A python list based implementation of a Stack ADT"""
def __init__(self):
"""creates an empty stack"""
self._data = []
def __len__(self):
"""returns: the number of elements in the stack"""
return len(self._data)
def isEmpty(self):
"""returns: true if the stack is empty, false otherwise"""
return len(self._data) == 0
def push(self, e):
"""adds an element to the top of stack"""
self._data.append(e)
def pop(self):
"""
returns: the element remove from the top of the stack
raises: an Empty exception if the stack is empty
"""
if self.isEmpty():
raise Empty('Stack is empty')
return self._data.pop()
def top(self):
"""
returns: the element at the top of the stack without removing it
raises: an Empty exception if the stack is empty
"""
if self.isEmpty():
raise Empty('Stack is empty')
return self._data[-1]