Skip to content

Commit 266c795

Browse files
committed
Extract Singly Linked List Node (#168)
1 parent d18d954 commit 266c795

5 files changed

Lines changed: 87 additions & 69 deletions

File tree

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
1-
from data_structures.singly_linked_list import Node
1+
from typing import Optional
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
23

34

45
class CircularlyLinkedList:
5-
def __init__(self, node=None):
6-
self.__head = node
6+
def __init__(self, node: Optional[SinglyLinkedListNode] = None):
7+
self.__head: SinglyLinkedListNode = node
78
if node:
8-
node.next_node = self.__head
9+
node.next = self.__head
910

1011
@property
11-
def head(self) -> Node:
12+
def head(self) -> SinglyLinkedListNode:
1213
return self.__head
1314

1415
@head.setter
15-
def head(self, node: Node):
16+
def head(self, node: SinglyLinkedListNode):
1617
self.__head = node
1718
if node:
18-
node.next_node = self.__head
19+
node.next = self.__head
1920

2021
def all_values(self) -> []:
2122
values = []
2223
node = self.head
2324

2425
while node:
2526
values.append(node.data)
26-
node = node.next_node
27+
node = node.next
2728
if node == self.head:
2829
break
2930
return values
3031

31-
def append(self, node: Node):
32+
def append(self, node: SinglyLinkedListNode):
3233
previous_node = self.head
3334

3435
if not previous_node:
3536
self.head = node
36-
node.next_node = self.head
37+
node.next = self.head
3738
return
3839

39-
while previous_node.next_node != self.head:
40-
previous_node = previous_node.next_node
40+
while previous_node.next != self.head:
41+
previous_node = previous_node.next
4142

42-
next_node = previous_node.next_node
43-
previous_node.next_node = node
44-
node.next_node = next_node
43+
next_node = previous_node.next
44+
previous_node.next = node
45+
node.next = next_node
4546

4647
def remove(self, index: int):
4748
previous_node = self.head
4849

4950
if not previous_node:
5051
raise IndexError("List is empty")
5152

52-
if previous_node.next_node == self.head:
53+
if previous_node.next == self.head:
5354
self.head = None
5455
return
5556

5657
if index == 0:
57-
previous_node.next_node = self.head.next_node
58-
self.head = previous_node.next_node
58+
previous_node.next = self.head.next
59+
self.head = previous_node.next
5960
return
6061

6162
list_index = 1
62-
while previous_node.next_node is not self.head and list_index < index:
63-
previous_node = previous_node.next_node
63+
while previous_node.next is not self.head and list_index < index:
64+
previous_node = previous_node.next
6465
list_index += 1
6566

6667
if list_index == index:
67-
next_node = previous_node.next_node
68-
previous_node.next_node = next_node.next_node
68+
next_node = previous_node.next
69+
previous_node.next = next_node.next
6970
else:
7071
raise IndexError
7172

@@ -75,9 +76,9 @@ def size(self) -> int:
7576
return count
7677

7778
count += 1
78-
node = self.head.next_node
79+
node = self.head.next
7980

8081
while node and node != self.head:
8182
count += 1
82-
node = node.next_node
83+
node = node.next
8384
return count
Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,61 @@
11
from typing import Generic, Optional, TypeVar
22

3-
T = TypeVar('T')
4-
5-
6-
class Node(Generic[T]):
7-
def __init__(self, data: T = None, next_node: 'Node' = None):
8-
self.data = data
9-
self.next_node = next_node
3+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
104

11-
12-
# Wikipedia: Singly linked lists contain nodes which have a data field as well as 'next' field, which points to the next
13-
# node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and
14-
# traversal.
5+
T = TypeVar('T')
156

167

178
class SinglyLinkedList(Generic[T]):
189

1910
def __init__(self):
20-
self.__head = Node()
11+
self.__head: SinglyLinkedListNode = SinglyLinkedListNode()
2112

2213
def all_values(self) -> [T]:
2314
values = []
24-
node = self.__head.next_node
15+
node = self.__head.next
2516

2617
while node:
2718
values.append(node.data)
28-
node = node.next_node
19+
node = node.next
2920

3021
return values
3122

3223
# O(N)
3324
def append(self, data: T) -> None:
34-
node = Node(data)
25+
node = SinglyLinkedListNode(data)
3526
last_node = self.__head
3627

3728
while last_node:
38-
if not last_node.next_node:
29+
if not last_node.next:
3930
break
40-
last_node = last_node.next_node
41-
last_node.next_node = node
31+
last_node = last_node.next
32+
last_node.next = node
4233

4334
# O(N)
4435
def get(self, index: int) -> Optional[T]:
45-
current_node = self.__head.next_node
36+
current_node = self.__head.next
4637
count = 0
4738

4839
while current_node:
4940
if count == index:
5041
break
51-
current_node = current_node.next_node
42+
current_node = current_node.next
5243
count += 1
5344
if current_node is None or count != index:
5445
return None
5546
return current_node.data
5647

5748
def insert(self, data: T, index: int) -> None:
58-
node = Node(data)
49+
node = SinglyLinkedListNode(data)
5950
current_node = self.__head
6051
count = 0
6152
while current_node:
6253
if count == index:
63-
next_node = current_node.next_node
64-
node.next_node = next_node
65-
current_node.next_node = node
54+
next_node = current_node.next
55+
node.next = next_node
56+
current_node.next = node
6657
return
67-
current_node = current_node.next_node
58+
current_node = current_node.next
6859
count += 1
6960
if count < index:
7061
IndexError('Index out of bounds')
@@ -75,20 +66,20 @@ def remove(self, index: int) -> None:
7566

7667
while current_node:
7768
if count == index:
78-
remove_node = current_node.next_node
79-
if remove_node and remove_node.next_node:
80-
current_node.next_node = remove_node.next_node
69+
remove_node = current_node.next
70+
if remove_node and remove_node.next:
71+
current_node.next = remove_node.next
8172
return
82-
current_node.next_node = None
83-
current_node = current_node.next_node
73+
current_node.next = None
74+
current_node = current_node.next
8475
count += 1
8576

8677
# O(N)
8778
def size(self) -> int:
8879
count = 0
89-
current_node = self.__head.next_node
80+
current_node = self.__head.next
9081

9182
while current_node:
9283
count += 1
93-
current_node = current_node.next_node
84+
current_node = current_node.next
9485
return count
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Generic, Optional, TypeVar
2+
3+
4+
T = TypeVar('T')
5+
6+
7+
class SinglyLinkedListNode(Generic[T]):
8+
def __init__(self, data: T = None, next_node: Optional['SinglyLinkedListNode'] = None):
9+
self.data = data
10+
self.next = next_node

‎data_structures/tests/test_circularly_linked_list.py‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from data_structures.circularly_linked_list import CircularlyLinkedList, Node
1+
from data_structures.circularly_linked_list import CircularlyLinkedList
2+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
23

34
EMPTY_LIST = CircularlyLinkedList()
4-
ONE_NODE_LIST = CircularlyLinkedList(Node(1))
5-
MULTI_NODE_LIST = CircularlyLinkedList(Node(1))
6-
MULTI_NODE_LIST.append(Node(2))
7-
MULTI_NODE_LIST.append(Node(3))
5+
ONE_NODE_LIST = CircularlyLinkedList(SinglyLinkedListNode(1))
6+
MULTI_NODE_LIST = CircularlyLinkedList(SinglyLinkedListNode(1))
7+
MULTI_NODE_LIST.append(SinglyLinkedListNode(2))
8+
MULTI_NODE_LIST.append(SinglyLinkedListNode(3))
89

910

1011
def test_init():
@@ -18,7 +19,7 @@ def test_tail():
1819
assert not EMPTY_LIST.head
1920
assert ONE_NODE_LIST.head
2021
test_list = CircularlyLinkedList()
21-
test_list.head = Node(1)
22+
test_list.head = SinglyLinkedListNode(1)
2223
assert test_list.size() == 1
2324

2425

@@ -30,22 +31,22 @@ def test_all_values():
3031

3132
def test_append():
3233
test_list = CircularlyLinkedList()
33-
test_list.append(Node(1))
34+
test_list.append(SinglyLinkedListNode(1))
3435
assert test_list.all_values() == [1]
3536

36-
test_list.append(Node(2))
37+
test_list.append(SinglyLinkedListNode(2))
3738
assert test_list.all_values() == [1, 2]
3839

39-
test_list.append(Node(3))
40+
test_list.append(SinglyLinkedListNode(3))
4041
assert test_list.all_values() == [1, 2, 3]
4142

4243

4344
def test_remove():
4445
test_list = CircularlyLinkedList()
45-
test_list.append(Node(1))
46-
test_list.append(Node(2))
47-
test_list.append(Node(3))
48-
test_list.append(Node(4))
46+
test_list.append(SinglyLinkedListNode(1))
47+
test_list.append(SinglyLinkedListNode(2))
48+
test_list.append(SinglyLinkedListNode(3))
49+
test_list.append(SinglyLinkedListNode(4))
4950

5051
test_list.remove(3)
5152
assert test_list.all_values() == [1, 2, 3]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from data_structures.singly_linked_list_node import SinglyLinkedListNode
2+
3+
4+
def test_singly_linked_list_node():
5+
node = SinglyLinkedListNode()
6+
assert node.next is None
7+
assert node.data is None
8+
9+
node = SinglyLinkedListNode(1)
10+
assert node.data == 1
11+
assert not node.next
12+
13+
node = SinglyLinkedListNode(1, SinglyLinkedListNode(2))
14+
assert node.data == 1
15+
assert node.next.data == 2

0 commit comments

Comments
 (0)