-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackList.py
48 lines (38 loc) · 1.09 KB
/
StackList.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
#!/usr/bin/env python
from DCList import DCList
class StackList(DCList):
""" StackList Class
"""
def __init__(self):
""" Constructor
Calls init from DCList
"""
DCList.__init__(self)
def push(self, data):
""" Push an object onto the StackList
:param data: object to push
"""
self.insertBefore(data)
def pop(self):
""" Pops an object from the StackList
:return: The top object on the StackList
"""
tail_data = self.peek()
self.remove(self.length-1)
return tail_data
def peek(self):
""" Shows the top element of the StackList, without removing it.
:return: Top object on the StackList
"""
if self.head is None:
raise IndexError("Empty StackList")
return self.tail.data
def purge(self):
""" Remove all elements from the StackList
"""
self.__init__()
def __len__(self):
""" Overload.
:return: The number of elements in the StackList
"""
return self.length