-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.py
More file actions
72 lines (58 loc) · 1.88 KB
/
Copy pathstack.py
File metadata and controls
72 lines (58 loc) · 1.88 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Stack:
"""Models a FILO stack
"""
def __init__(self):
"""The constructor
"""
self.data = list() # create an empty list
def push(self, value):
"""Push the value on top of the stack
:param value: The value to be pushed to the stack
:type value: any
"""
self.data.append(value)
def pop(self):
"""Remove the value on the top of the stack
:raises IndexError: The stack is empty
:return: The value at the top of the stack
:rtype: any
"""
if len(self.data) == 0:
raise IndexError('Attempt to pop an empty stack')
value = self.data.pop() # from last index
return value
def isEmpty(self) -> bool:
"""Returns True if the queue is empty
:return: True if the queue is empty
:rtype: bool
"""
return len(self.data) == 0
def isFull(self) -> bool:
"""Returns True if the queue is full
:return: True if the queue is full
:rtype: bool
"""
return False
def print(self, sep=','):
""" Print the content of the queue
:param sep: The separating character between the values in the queue, defaults to ','
:type sep: str, optional
"""
isFirst = True
for value in self.data:
if not isFirst:
print(sep, end='')
print(value, end='')
isFirst = False
print() # add a new line at the end
if __name__ == "__main__":
theStack = Stack()
theStack.push(20)
theStack.push(15)
theStack.push(36)
theStack.push(8)
theStack.push(26)
print("Content of the stack (top at the end): ", end='')
theStack.print()
while not theStack.isEmpty():
print(theStack.pop())