Skip to content

Commit 1258d80

Browse files
PreciousJac0bpre-commit-ci[bot]cclauss
authored andcommitted
Add tests to data_structures/linked_list/swap_nodes.py (TheAlgorithms#10751)
* Added doctests to the swap_nodes file under linkedlist data structure * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added doctests to the swap_nodes file under linkedlist data structure * Added doctests to the swap_nodes file under linkedlist data structure * Added doctests to the swap_nodes file under linkedlist data structure * Update swap_nodes.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 2761bc2 commit 1258d80

File tree

1 file changed

+108
-44
lines changed

1 file changed

+108
-44
lines changed
+108-44
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,148 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Iterator
4+
from dataclasses import dataclass
15
from typing import Any
26

37

8+
@dataclass
49
class Node:
5-
def __init__(self, data: Any) -> None:
6-
"""
7-
Initialize a new Node with the given data.
8-
9-
Args:
10-
data: The data to be stored in the node.
11-
12-
"""
13-
self.data = data
14-
self.next: Node | None = None # Reference to the next node
10+
data: Any
11+
next_node: Node | None = None
1512

1613

14+
@dataclass
1715
class LinkedList:
18-
def __init__(self) -> None:
16+
head: Node | None = None
17+
18+
def __iter__(self) -> Iterator:
1919
"""
20-
Initialize an empty Linked List.
20+
>>> linked_list = LinkedList()
21+
>>> list(linked_list)
22+
[]
23+
>>> linked_list.push(0)
24+
>>> tuple(linked_list)
25+
(0,)
2126
"""
22-
self.head: Node | None = None # Reference to the head (first node)
27+
node = self.head
28+
while node:
29+
yield node.data
30+
node = node.next_node
2331

24-
def print_list(self):
32+
def __len__(self) -> int:
2533
"""
26-
Print the elements of the Linked List in order.
34+
>>> linked_list = LinkedList()
35+
>>> len(linked_list)
36+
0
37+
>>> linked_list.push(0)
38+
>>> len(linked_list)
39+
1
2740
"""
28-
temp = self.head
29-
while temp is not None:
30-
print(temp.data, end=" ")
31-
temp = temp.next
32-
print()
41+
return sum(1 for _ in self)
3342

3443
def push(self, new_data: Any) -> None:
3544
"""
3645
Add a new node with the given data to the beginning of the Linked List.
46+
3747
Args:
3848
new_data (Any): The data to be added to the new node.
49+
50+
Returns:
51+
None
52+
53+
Examples:
54+
>>> linked_list = LinkedList()
55+
>>> linked_list.push(5)
56+
>>> linked_list.push(4)
57+
>>> linked_list.push(3)
58+
>>> linked_list.push(2)
59+
>>> linked_list.push(1)
60+
>>> list(linked_list)
61+
[1, 2, 3, 4, 5]
3962
"""
4063
new_node = Node(new_data)
41-
new_node.next = self.head
64+
new_node.next_node = self.head
4265
self.head = new_node
4366

44-
def swap_nodes(self, node_data_1, node_data_2) -> None:
67+
def swap_nodes(self, node_data_1: Any, node_data_2: Any) -> None:
4568
"""
4669
Swap the positions of two nodes in the Linked List based on their data values.
70+
4771
Args:
4872
node_data_1: Data value of the first node to be swapped.
4973
node_data_2: Data value of the second node to be swapped.
5074
5175
5276
Note:
5377
If either of the specified data values isn't found then, no swapping occurs.
78+
79+
Examples:
80+
When both values are present in a linked list.
81+
>>> linked_list = LinkedList()
82+
>>> linked_list.push(5)
83+
>>> linked_list.push(4)
84+
>>> linked_list.push(3)
85+
>>> linked_list.push(2)
86+
>>> linked_list.push(1)
87+
>>> list(linked_list)
88+
[1, 2, 3, 4, 5]
89+
>>> linked_list.swap_nodes(1, 5)
90+
>>> tuple(linked_list)
91+
(5, 2, 3, 4, 1)
92+
93+
When one value is present and the other isn't in the linked list.
94+
>>> second_list = LinkedList()
95+
>>> second_list.push(6)
96+
>>> second_list.push(7)
97+
>>> second_list.push(8)
98+
>>> second_list.push(9)
99+
>>> second_list.swap_nodes(1, 6) is None
100+
True
101+
102+
When both values are absent in the linked list.
103+
>>> second_list = LinkedList()
104+
>>> second_list.push(10)
105+
>>> second_list.push(9)
106+
>>> second_list.push(8)
107+
>>> second_list.push(7)
108+
>>> second_list.swap_nodes(1, 3) is None
109+
True
110+
111+
When linkedlist is empty.
112+
>>> second_list = LinkedList()
113+
>>> second_list.swap_nodes(1, 3) is None
114+
True
115+
116+
Returns:
117+
None
54118
"""
55119
if node_data_1 == node_data_2:
56120
return
57-
else:
58-
node_1 = self.head
59-
while node_1 is not None and node_1.data != node_data_1:
60-
node_1 = node_1.next
61-
62-
node_2 = self.head
63-
while node_2 is not None and node_2.data != node_data_2:
64-
node_2 = node_2.next
65-
66-
if node_1 is None or node_2 is None:
67-
return
68121

69-
# Swap the data values of the two nodes
70-
node_1.data, node_2.data = node_2.data, node_1.data
122+
node_1 = self.head
123+
while node_1 and node_1.data != node_data_1:
124+
node_1 = node_1.next_node
125+
node_2 = self.head
126+
while node_2 and node_2.data != node_data_2:
127+
node_2 = node_2.next_node
128+
if node_1 is None or node_2 is None:
129+
return
130+
# Swap the data values of the two nodes
131+
node_1.data, node_2.data = node_2.data, node_1.data
71132

72133

73134
if __name__ == "__main__":
74-
ll = LinkedList()
75-
for i in range(5, 0, -1):
76-
ll.push(i)
135+
"""
136+
Python script that outputs the swap of nodes in a linked list.
137+
"""
138+
from doctest import testmod
77139

78-
print("Original Linked List:")
79-
ll.print_list()
80-
81-
ll.swap_nodes(1, 4)
82-
print("After swapping the nodes whose data is 1 and 4:")
140+
testmod()
141+
linked_list = LinkedList()
142+
for i in range(5, 0, -1):
143+
linked_list.push(i)
83144

84-
ll.print_list()
145+
print(f"Original Linked List: {list(linked_list)}")
146+
linked_list.swap_nodes(1, 4)
147+
print(f"Modified Linked List: {list(linked_list)}")
148+
print("After swapping the nodes whose data is 1 and 4.")

0 commit comments

Comments
 (0)