Skip to content

Commit 5083231

Browse files
committed
Update Readme Files (#172)
1 parent d199e36 commit 5083231

3 files changed

Lines changed: 100 additions & 124 deletions

File tree

‎data_structures/README.md‎

Lines changed: 87 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -29,82 +29,66 @@ Drawbacks:
2929
Node Implementation:
3030

3131
```python
32-
class Node(Generic[T]):
33-
def __init__(self, data: T = None, next_node: 'Node' = None):
32+
class SinglyLinkedListNode(Generic[T]):
33+
def __init__(self, data: T = None, next_node: Optional['SinglyLinkedListNode'] = None):
3434
self.data = data
35-
self.next_node = next_node
35+
self.next = next_node
3636
```
3737

3838
Singly Linked List Implementation:
3939

4040
```python
41-
from typing import Generic, Optional, TypeVar
42-
43-
T = TypeVar('T')
44-
45-
46-
class Node(Generic[T]):
47-
def __init__(self, data: T = None, next_node: 'Node' = None):
48-
self.data = data
49-
self.next_node = next_node
50-
51-
52-
# Wikipedia: Singly linked lists contain nodes which have a data field as well as 'next' field, which points to the next
53-
# node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and
54-
# traversal.
55-
56-
5741
class SinglyLinkedList(Generic[T]):
5842

5943
def __init__(self):
60-
self.__head = Node()
44+
self.__head: SinglyLinkedListNode = SinglyLinkedListNode()
6145

6246
def all_values(self) -> [T]:
6347
values = []
64-
node = self.__head.next_node
48+
node = self.__head.next
6549

6650
while node:
6751
values.append(node.data)
68-
node = node.next_node
52+
node = node.next
6953

7054
return values
7155

7256
# O(N)
7357
def append(self, data: T) -> None:
74-
node = Node(data)
58+
node = SinglyLinkedListNode(data)
7559
last_node = self.__head
7660

7761
while last_node:
78-
if not last_node.next_node:
62+
if not last_node.next:
7963
break
80-
last_node = last_node.next_node
81-
last_node.next_node = node
64+
last_node = last_node.next
65+
last_node.next = node
8266

8367
# O(N)
8468
def get(self, index: int) -> Optional[T]:
85-
current_node = self.__head.next_node
69+
current_node = self.__head.next
8670
count = 0
8771

8872
while current_node:
8973
if count == index:
9074
break
91-
current_node = current_node.next_node
75+
current_node = current_node.next
9276
count += 1
9377
if current_node is None or count != index:
9478
return None
9579
return current_node.data
9680

9781
def insert(self, data: T, index: int) -> None:
98-
node = Node(data)
82+
node = SinglyLinkedListNode(data)
9983
current_node = self.__head
10084
count = 0
10185
while current_node:
10286
if count == index:
103-
next_node = current_node.next_node
104-
node.next_node = next_node
105-
current_node.next_node = node
87+
next_node = current_node.next
88+
node.next = next_node
89+
current_node.next = node
10690
return
107-
current_node = current_node.next_node
91+
current_node = current_node.next
10892
count += 1
10993
if count < index:
11094
IndexError('Index out of bounds')
@@ -115,22 +99,22 @@ class SinglyLinkedList(Generic[T]):
11599

116100
while current_node:
117101
if count == index:
118-
remove_node = current_node.next_node
119-
if remove_node and remove_node.next_node:
120-
current_node.next_node = remove_node.next_node
102+
remove_node = current_node.next
103+
if remove_node and remove_node.next:
104+
current_node.next = remove_node.next
121105
return
122-
current_node.next_node = None
123-
current_node = current_node.next_node
106+
current_node.next = None
107+
current_node = current_node.next
124108
count += 1
125109

126110
# O(N)
127111
def size(self) -> int:
128112
count = 0
129-
current_node = self.__head.next_node
113+
current_node = self.__head.next
130114

131115
while current_node:
132116
count += 1
133-
current_node = current_node.next_node
117+
current_node = current_node.next
134118
return count
135119
```
136120

@@ -142,11 +126,13 @@ contains, besides the next-node link, a second link field pointing to the 'previ
142126
Doubly Linked List Node:
143127

144128
```python
145-
class Node(Generic[T]):
146-
def __init__(self, data: T = None, previous_node: 'Node' = None, next_node: 'Node' = None):
147-
self.data = data
148-
self.previous_node = previous_node
149-
self.next_node = next_node
129+
class DoublyLinkedListNode(Generic[T]):
130+
def __init__(self, data: T = None,
131+
previous_node: Optional['DoublyLinkedListNode'] = None,
132+
next_node: Optional['DoublyLinkedListNode'] = None):
133+
self.data: T = data
134+
self.previous: Optional[DoublyLinkedListNode] = previous_node
135+
self.next: Optional[DoublyLinkedListNode] = next_node
150136
```
151137

152138
Implementation:
@@ -155,44 +141,44 @@ Implementation:
155141
class DoublyLinkedList(Generic[T]):
156142

157143
def __init__(self):
158-
self.__head = Node()
159-
self.__tail = Node()
144+
self.__head: DoublyLinkedListNode = DoublyLinkedListNode()
145+
self.__tail: DoublyLinkedListNode = DoublyLinkedListNode()
160146

161-
self.__head.next_node = self.__tail
162-
self.__tail.previous_node = self.__head
147+
self.__head.next = self.__tail
148+
self.__tail.previous = self.__head
163149

164150
# O(N)
165151
def all_values(self) -> [T]:
166152
values = []
167-
node = self.__head.next_node
153+
node = self.__head.next
168154

169155
while node is not self.__tail:
170156
values.append(node.data)
171-
node = node.next_node
157+
node = node.next
172158

173159
return values
174160

175161
# O(1)
176162
def append(self, data: T):
177163
# Last <-> Tail --> Last <-> New <-> Tail
178-
node = Node(data)
179-
last_node = self.__tail.previous_node
164+
node = DoublyLinkedListNode(data)
165+
last_node = self.__tail.previous
180166

181-
last_node.next_node = node
182-
node.previous_node = last_node
167+
last_node.next = node
168+
node.previous = last_node
183169

184-
node.next_node = self.__tail
185-
self.__tail.previous_node = node
170+
node.next = self.__tail
171+
self.__tail.previous = node
186172

187173
# O(N)
188-
def get_node(self, index: int) -> Optional[Node]:
189-
current_node = self.__head.next_node
174+
def get_node(self, index: int) -> Optional[DoublyLinkedListNode]:
175+
current_node = self.__head.next
190176
count = 0
191177

192178
while current_node is not self.__tail:
193179
if count == index:
194180
return current_node
195-
current_node = current_node.next_node
181+
current_node = current_node.next
196182
count += 1
197183
return None
198184

@@ -205,24 +191,24 @@ class DoublyLinkedList(Generic[T]):
205191

206192
# O(N)
207193
def insert(self, data: T, index: int):
208-
node = Node(data)
209-
current_node: Node = self.get_node(index)
210-
194+
node = DoublyLinkedListNode(data)
195+
current_node: DoublyLinkedListNode = self.get_node(index)
196+
211197
if not current_node and index == 0:
212-
self.__head.next_node = node
213-
node.previous_node = self.__head
214-
node.next_node = self.__tail
215-
self.__tail.previous_node = node
198+
self.__head.next = node
199+
node.previous = self.__head
200+
node.next = self.__tail
201+
self.__tail.previous = node
216202
return
217203

218204
if not current_node:
219205
raise IndexError("Index out of bounds")
220206

221207
# Previous <-> Original --> Previous <-> New <-> Original
222-
current_node.previous_node.next_node = node
223-
node.previous_node = current_node.previous_node
224-
node.next_node = current_node
225-
current_node.previous_node = node
208+
current_node.previous.next = node
209+
node.previous = current_node.previous
210+
node.next = current_node
211+
current_node.previous = node
226212

227213
# O(N)
228214
def remove(self, index: int) -> Optional[T]:
@@ -231,19 +217,19 @@ class DoublyLinkedList(Generic[T]):
231217
return None
232218

233219
# Previous <-> Node <-> Next --> Previous <-> Next
234-
node.previous_node.next_node = node.next_node
235-
node.next_node.previous_node = node.previous_node
220+
node.previous.next = node.next
221+
node.next.previous = node.previous
236222

237223
return node.data
238224

239225
# O(N)
240226
def size(self) -> int:
241227
count = 0
242-
node = self.__head.next_node
228+
node = self.__head.next
243229

244230
while node != self.__tail:
245231
count += 1
246-
node = node.next_node
232+
node = node.next
247233
return count
248234
```
249235

@@ -257,71 +243,71 @@ pointer points to the first node.
257243
This is more of an example. It can be optimized in a variety of ways depending on its intended usage.
258244

259245
```python
260-
class CircularlyLinkedList(object):
261-
def __init__(self, node=None):
262-
self._head = node
246+
class CircularlyLinkedList:
247+
def __init__(self, node: Optional[SinglyLinkedListNode] = None):
248+
self.__head: SinglyLinkedListNode = node
263249
if node:
264-
node.next_node = self._head
250+
node.next = self.__head
265251

266252
@property
267-
def head(self) -> Node:
268-
return self._head
253+
def head(self) -> SinglyLinkedListNode:
254+
return self.__head
269255

270256
@head.setter
271-
def head(self, node: Node):
272-
self._head = node
257+
def head(self, node: SinglyLinkedListNode):
258+
self.__head = node
273259
if node:
274-
node.next_node = self._head
260+
node.next = self.__head
275261

276262
def all_values(self) -> []:
277263
values = []
278264
node = self.head
279265

280266
while node:
281267
values.append(node.data)
282-
node = node.next_node
268+
node = node.next
283269
if node == self.head:
284270
break
285271
return values
286272

287-
def append(self, node: Node):
273+
def append(self, node: SinglyLinkedListNode):
288274
previous_node = self.head
289275

290276
if not previous_node:
291277
self.head = node
292-
node.next_node = self.head
278+
node.next = self.head
293279
return
294280

295-
while previous_node.next_node != self.head:
296-
previous_node = previous_node.next_node
281+
while previous_node.next != self.head:
282+
previous_node = previous_node.next
297283

298-
next_node = previous_node.next_node
299-
previous_node.next_node = node
300-
node.next_node = next_node
284+
next_node = previous_node.next
285+
previous_node.next = node
286+
node.next = next_node
301287

302288
def remove(self, index: int):
303289
previous_node = self.head
304290

305291
if not previous_node:
306292
raise IndexError("List is empty")
307293

308-
if previous_node.next_node == self.head:
294+
if previous_node.next == self.head:
309295
self.head = None
310296
return
311297

312298
if index == 0:
313-
previous_node.next_node = self.head.next_node
314-
self.head = previous_node.next_node
299+
previous_node.next = self.head.next
300+
self.head = previous_node.next
315301
return
316302

317303
list_index = 1
318-
while previous_node.next_node is not self.head and list_index < index:
319-
previous_node = previous_node.next_node
304+
while previous_node.next is not self.head and list_index < index:
305+
previous_node = previous_node.next
320306
list_index += 1
321307

322308
if list_index == index:
323-
next_node = previous_node.next_node
324-
previous_node.next_node = next_node.next_node
309+
next_node = previous_node.next
310+
previous_node.next = next_node.next
325311
else:
326312
raise IndexError
327313

@@ -331,11 +317,11 @@ class CircularlyLinkedList(object):
331317
return count
332318

333319
count += 1
334-
node = self.head.next_node
320+
node = self.head.next
335321

336322
while node and node != self.head:
337323
count += 1
338-
node = node.next_node
324+
node = node.next
339325
return count
340326
```
341327

‎doubly_linked_list/list_node.py‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)