-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
51 lines (39 loc) · 1.21 KB
/
stack.py
File metadata and controls
51 lines (39 loc) · 1.21 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
43
44
45
46
47
48
49
50
51
'''
python stack implementation
@author: jvallver [jvallver@gmail.com]
'''
class EmptyStackError(Exception): pass
class stack(object):
def __init__(self):
self.__head = None
def push(self, value):
self.__head = _StackItem(value, self.__head)
def pop(self):
if self.__head is None: raise EmptyStackError("The stack is empty!")
value = self.__head.value
self.__head = self.__head.nextItem
return value
def isEmpty(self):
return self.__head is None
def __str__(self):
currentItem = self.__head
string = ""
while currentItem:
if type(currentItem.value) is str:
string += "'{0}'".format(currentItem.value)
else:
string += str(currentItem.value)
currentItem = currentItem.nextItem
if currentItem:
string += ", "
return "[{0}]".format(string)
class _StackItem():
def __init__(self, value, nextItem):
self.__value = value
self.__nextItem = nextItem
@property
def value(self):
return self.__value
@property
def nextItem(self):
return self.__nextItem