Skip to content

Commit cd600f8

Browse files
committed
Extract Doubly Linked List Node (#169)
1 parent 266c795 commit cd600f8

3 files changed

Lines changed: 61 additions & 41 deletions

File tree

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,50 @@
11
from typing import Generic, Optional, TypeVar
2+
from data_structures.doubly_linked_list_node import DoublyLinkedListNode
23

34
T = TypeVar('T')
45

56

6-
class Node(Generic[T]):
7-
def __init__(self, data: T = None, previous_node: 'Node' = None, next_node: 'Node' = None):
8-
self.data = data
9-
self.previous_node = previous_node
10-
self.next_node = next_node
11-
12-
13-
# Wikipedia: In a 'doubly linked list', each node contains, besides the next-node link, a second link field pointing
14-
# to the 'previous' node in the sequence. The two links may be called 'forward('s') and 'backwards', or 'next' and
15-
# 'prev'('previous').
16-
17-
187
class DoublyLinkedList(Generic[T]):
198

209
def __init__(self):
21-
self.__head = Node()
22-
self.__tail = Node()
10+
self.__head: DoublyLinkedListNode = DoublyLinkedListNode()
11+
self.__tail: DoublyLinkedListNode = DoublyLinkedListNode()
2312

24-
self.__head.next_node = self.__tail
25-
self.__tail.previous_node = self.__head
13+
self.__head.next = self.__tail
14+
self.__tail.previous = self.__head
2615

2716
# O(N)
2817
def all_values(self) -> [T]:
2918
values = []
30-
node = self.__head.next_node
19+
node = self.__head.next
3120

3221
while node is not self.__tail:
3322
values.append(node.data)
34-
node = node.next_node
23+
node = node.next
3524

3625
return values
3726

3827
# O(1)
3928
def append(self, data: T):
4029
# Last <-> Tail --> Last <-> New <-> Tail
41-
node = Node(data)
42-
last_node = self.__tail.previous_node
30+
node = DoublyLinkedListNode(data)
31+
last_node = self.__tail.previous
4332

44-
last_node.next_node = node
45-
node.previous_node = last_node
33+
last_node.next = node
34+
node.previous = last_node
4635

47-
node.next_node = self.__tail
48-
self.__tail.previous_node = node
36+
node.next = self.__tail
37+
self.__tail.previous = node
4938

5039
# O(N)
51-
def get_node(self, index: int) -> Optional[Node]:
52-
current_node = self.__head.next_node
40+
def get_node(self, index: int) -> Optional[DoublyLinkedListNode]:
41+
current_node = self.__head.next
5342
count = 0
5443

5544
while current_node is not self.__tail:
5645
if count == index:
5746
return current_node
58-
current_node = current_node.next_node
47+
current_node = current_node.next
5948
count += 1
6049
return None
6150

@@ -68,24 +57,24 @@ def get(self, index: int) -> Optional[T]:
6857

6958
# O(N)
7059
def insert(self, data: T, index: int):
71-
node = Node(data)
72-
current_node: Node = self.get_node(index)
60+
node = DoublyLinkedListNode(data)
61+
current_node: DoublyLinkedListNode = self.get_node(index)
7362

7463
if not current_node and index == 0:
75-
self.__head.next_node = node
76-
node.previous_node = self.__head
77-
node.next_node = self.__tail
78-
self.__tail.previous_node = node
64+
self.__head.next = node
65+
node.previous = self.__head
66+
node.next = self.__tail
67+
self.__tail.previous = node
7968
return
8069

8170
if not current_node:
8271
raise IndexError("Index out of bounds")
8372

8473
# Previous <-> Original --> Previous <-> New <-> Original
85-
current_node.previous_node.next_node = node
86-
node.previous_node = current_node.previous_node
87-
node.next_node = current_node
88-
current_node.previous_node = node
74+
current_node.previous.next = node
75+
node.previous = current_node.previous
76+
node.next = current_node
77+
current_node.previous = node
8978

9079
# O(N)
9180
def remove(self, index: int) -> Optional[T]:
@@ -94,17 +83,17 @@ def remove(self, index: int) -> Optional[T]:
9483
return None
9584

9685
# Previous <-> Node <-> Next --> Previous <-> Next
97-
node.previous_node.next_node = node.next_node
98-
node.next_node.previous_node = node.previous_node
86+
node.previous.next = node.next
87+
node.next.previous = node.previous
9988

10089
return node.data
10190

10291
# O(N)
10392
def size(self) -> int:
10493
count = 0
105-
node = self.__head.next_node
94+
node = self.__head.next
10695

10796
while node != self.__tail:
10897
count += 1
109-
node = node.next_node
98+
node = node.next
11099
return count
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Generic, Optional, TypeVar
2+
3+
4+
T = TypeVar('T')
5+
6+
7+
class DoublyLinkedListNode(Generic[T]):
8+
def __init__(self, data: T = None,
9+
previous_node: Optional['DoublyLinkedListNode'] = None,
10+
next_node: Optional['DoublyLinkedListNode'] = None):
11+
self.data: T = data
12+
self.previous: Optional[DoublyLinkedListNode] = previous_node
13+
self.next: Optional[DoublyLinkedListNode] = next_node
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from data_structures.doubly_linked_list_node import DoublyLinkedListNode
2+
3+
4+
def test_doubly_linked_list_node():
5+
node = DoublyLinkedListNode()
6+
assert node.data is None
7+
assert node.next is None
8+
assert node.previous is None
9+
10+
node = DoublyLinkedListNode(1)
11+
assert node.data == 1
12+
assert node.previous is None
13+
assert node.next is None
14+
15+
node = DoublyLinkedListNode(2, DoublyLinkedListNode(1), DoublyLinkedListNode(3))
16+
assert node.data == 2
17+
assert node.previous.data == 1
18+
assert node.next.data == 3

0 commit comments

Comments
 (0)