|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Iterator |
| 4 | +from dataclasses import dataclass |
1 | 5 | from typing import Any
|
2 | 6 |
|
3 | 7 |
|
| 8 | +@dataclass |
4 | 9 | 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 |
15 | 12 |
|
16 | 13 |
|
| 14 | +@dataclass |
17 | 15 | class LinkedList:
|
18 |
| - def __init__(self) -> None: |
| 16 | + head: Node | None = None |
| 17 | + |
| 18 | + def __iter__(self) -> Iterator: |
19 | 19 | """
|
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,) |
21 | 26 | """
|
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 |
23 | 31 |
|
24 |
| - def print_list(self): |
| 32 | + def __len__(self) -> int: |
25 | 33 | """
|
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 |
27 | 40 | """
|
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) |
33 | 42 |
|
34 | 43 | def push(self, new_data: Any) -> None:
|
35 | 44 | """
|
36 | 45 | Add a new node with the given data to the beginning of the Linked List.
|
| 46 | +
|
37 | 47 | Args:
|
38 | 48 | 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] |
39 | 62 | """
|
40 | 63 | new_node = Node(new_data)
|
41 |
| - new_node.next = self.head |
| 64 | + new_node.next_node = self.head |
42 | 65 | self.head = new_node
|
43 | 66 |
|
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: |
45 | 68 | """
|
46 | 69 | Swap the positions of two nodes in the Linked List based on their data values.
|
| 70 | +
|
47 | 71 | Args:
|
48 | 72 | node_data_1: Data value of the first node to be swapped.
|
49 | 73 | node_data_2: Data value of the second node to be swapped.
|
50 | 74 |
|
51 | 75 |
|
52 | 76 | Note:
|
53 | 77 | 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 |
54 | 118 | """
|
55 | 119 | if node_data_1 == node_data_2:
|
56 | 120 | 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 |
68 | 121 |
|
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 |
71 | 132 |
|
72 | 133 |
|
73 | 134 | 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 |
77 | 139 |
|
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) |
83 | 144 |
|
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