-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayStack.py
More file actions
42 lines (26 loc) · 1.2 KB
/
ArrayStack.py
File metadata and controls
42 lines (26 loc) · 1.2 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
39
40
41
42
class ArrayStack(object):
""" LIFO Stack implementation using a Python list as underlying storage. """
def __init__(self):
""" make an empty stack. """
self._data = [] # non-public list instance
def __len__(self):
"""Return the # of elements in the stack. """
return len(self._data)
def is_empty(self):
""" Return True is stack is empty. """
return len(self._data) == 0
def push(self, e):
""" Add element e to the top of the stack. """
self._data.append(e) # new item stored at end of list
def top(self):
""" Return (but do not remove) the element at the top of the stack.
Raise Empty exception if the stack is empty.
"""
if self.is_empty():
raise ('Stack is empty!')
return self._data[-1] # the last item in the list
def pop(self):
""" Remove and return the element from the top of the stack (i.e., LIFO)."""
if self.is_empty():
raise ('Stack is empty!')
return self._data.pop()