-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_sll.py
138 lines (119 loc) · 3.91 KB
/
stack_sll.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Name: Eric Hauschild
# OSU Email: [email protected]
# Course: CS261 - Data Structures
# Assignment: 3
# Due Date: 5/2/2022
# Description: This program contains the class for a stack implemented with nodes with methods I created below the
# dotted line.
from SLNode import SLNode
class StackException(Exception):
"""
Custom exception to be used by Stack class
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
pass
class Stack:
def __init__(self) -> None:
"""
Initialize new stack with head node
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._head = None
def __str__(self) -> str:
"""
Return content of stack in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = 'STACK ['
if not self.is_empty():
node = self._head
out = out + str(node.value)
node = node.next
while node:
out = out + ' -> ' + str(node.value)
node = node.next
return out + ']'
def is_empty(self) -> bool:
"""
Return True is the stack is empty, False otherwise
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._head is None
def size(self) -> int:
"""
Return number of elements currently in the stack
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
node = self._head
length = 0
while node:
length += 1
node = node.next
return length
# -----------------------------------------------------------------------
def push(self, value: object) -> None:
"""
This method adds a new element to the top of the stack. It must be implemented with O(1)
runtime complexity.
"""
if self.is_empty() is True:
self._head = SLNode(value)
else:
old_head = self._head
self._head = SLNode(value, old_head)
def pop(self) -> object:
"""
This method removes the top element from the stack and returns its value. It must be
implemented with O(1) runtime complexity. If the stack is empty, the method raises a
custom “StackException”. Code for the exception is provided in the skeleton file.
"""
if self.is_empty() is True:
raise StackException()
else:
value_to_pop = self._head.value
self._head = self._head.next
return value_to_pop
def top(self) -> object:
"""
This method returns the value of the top element of the stack without removing it. It must
be implemented with O(1) runtime complexity. If the stack is empty, the method raises a
custom “StackException”. Code for the exception is provided in the skeleton file.
"""
if self.is_empty() is True:
raise StackException()
else:
value_to_pop = self._head.value
return value_to_pop
# ------------------- BASIC TESTING -----------------------------------------
if __name__ == "__main__":
print("\n# push example 1")
s = Stack()
print(s)
for value in [1, 2, 3, 4, 5]:
s.push(value)
print(s)
print("\n# pop example 1")
s = Stack()
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
for value in [1, 2, 3, 4, 5]:
s.push(value)
for i in range(6):
try:
print(s.pop())
except Exception as e:
print("Exception:", type(e))
print("\n# top example 1")
s = Stack()
try:
s.top()
except Exception as e:
print("No elements in stack", type(e))
s.push(10)
s.push(20)
print(s)
print(s.top())
print(s.top())
print(s)