-
Notifications
You must be signed in to change notification settings - Fork 14
/
implementation.py
215 lines (178 loc) · 5.55 KB
/
implementation.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
Linked List implementation
At the core of the linked list data structure is the Node class.
"""
from typing import List
class Node:
"""
A node is a container that provides the ability to both store data and connect to other nodes.
+-----+------+
| 3 | None +
+-----+------+
first = Node(3)
+-----+------+ +-----+------+
| 3 | None + | 5 | None +
+-----+------+ +-----+------+
second = Node(5)
+-----+------+ +-----+------+
| 3 | *---+--->| 5 | None +
+-----+------+ +-----+------+
first.next = second
+-----+------+ +-----+------+ +-----+------+
| 3 | *---+--->| 5 | *---+-->| 7 | None +
+-----+------+ +-----+------+ +-----+------+
third = Node(7)
second.next = third
"""
def __init__(self, data) -> None:
self.data = data
self.next = None
class LinkedList:
"""
Linked List implementation
Methods implemented
- add(item: _type_) -> None
- insert(item: _type_, index:int) -> None
- get(index: int) -> _type_
- clear() -> None
- contains(item: _type_) -> bool
- copy_to(list: List, index: int) -> None
- remove(item: _type_) -> None
- is_empty() -> bool
- length() -> int
"""
def __init__(self) -> None:
self._first = None
self._count = 0
def add(self, item) -> None:
"""
Adds a new element to the end of the linked list
Performance: O(n)
Args:
item (_type_): Element to be added
"""
node = Node(item)
if cursor := self._first:
while cursor:
previous = cursor
cursor = cursor.next
previous.next = node
else:
self._first = node
self._count += 1
def insert(self, item, index: int) -> None:
"""
Insert a new element in a given position
Performance: O(n)
Args:
item (_type_): New element to be inserted
index (int): Position where it will be inserted
Raises:
ValueError: Index out of range
"""
if index <= 0:
raise ValueError("Index out of range")
node = Node(item)
if cursor := self._first:
current = 1
while cursor and current != index:
previous = cursor
cursor = cursor.next
current += 1
previous.next = node
node.next = cursor
else:
cursor = node
self._count += 1
def get(self, index: int):
"""
Returns the element located at the given position
Performance: O(n)
Args:
index (int): Position of the element to return
Raises:
ValueError: Index out of range
"""
if index <= 0 or index > self._count:
raise ValueError("index out of range")
if cursor := self._first:
current = 1
while cursor and index != current:
current += 1
cursor = cursor.next
return cursor.data
return None
def clear(self) -> None:
"""
Removes all the items from the list.
Performance: O(1)
"""
self._first = None
self._count = 0
def contains(self, item) -> bool:
"""Checks whether the provided value exists within the linked list.
Performance: O(n)
Returns:
bool: Bool value indicating the existence of a given element.
"""
cursor = self._first
while cursor:
if cursor.data == item:
return True
cursor = cursor.next
return False
def copy_to(self, to_copy_list: List, index: int) -> None:
"""Updates the list passed as a parameter with the elements of the linked list starting at `index`.
Performance: O(n)
Args:
to_copy_list (List): List to be updated
index (int): Starting position
Raises:
ValueError: Index out of range
"""
if index <= 0 or index > self._count:
raise ValueError("Index out of range")
cursor = self._first
current = 1
while cursor:
if current >= index:
to_copy_list.append(cursor.data)
cursor = cursor.next
current += 1
def remove(self, index: int):
"""
Removes and return the element located at the given position.
Performance: O(n)
Args:
index (int): Position of the element to be deleted
Raises:
ValueError: Index out of range
"""
if index <= 0 or index > self._count:
raise ValueError("Index out of range")
cursor = self._first
if cursor:
current = 1
while index != current:
current += 1
previous = cursor
cursor = cursor.next
previous.next = cursor.next
self._count -= 1
return cursor.data
def is_empty(self) -> bool:
"""
Checks if the linked list is empty.
Performance: O(1)
Returns:
bool: True if the linked list is empty, False otherwise.
"""
return self._count == 0
def length(self) -> int:
"""
Returns the total number of elements contained in the linked list
Performance: O(1)
Returns:
int: Total number of elements contained in the linked list
"""
return self._count