bi_directional_links
BiDirection
BiDirection(value: Any)
Bases: Generic[_BiDirectionT]
Create a bi-directional linked node.
Initializes a node with the given value and sets its previous and next pointers to itself, forming a circular structure when isolated.
Parameters:
-
value(Any) –The value to store in the node.
Examples:
>>> node = BiDirection("A")
>>> node.value
'A'
>>> node.prev is node
True
>>> node.next is node
True
Source code in ures/data_structure/bi_directional_links.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
prev
property
prev: _BiDirectionT
Get the previous node in the linked structure.
Returns:
-
BiDirection(_BiDirectionT) –The previous node.
Examples:
>>> node = BiDirection("A")
>>> node.prev is node
True
next
property
next: _BiDirectionT
Get the next node in the linked structure.
Returns:
-
BiDirection(_BiDirectionT) –The next node.
Examples:
>>> node = BiDirection("A")
>>> node.next is node
True
value
property
value: Any
Retrieve the value stored in the node.
Returns:
-
Any(Any) –The node's value.
Examples:
>>> node = BiDirection(123)
>>> node.value
123
id
property
id: str
Get the unique identifier of the node.
Returns:
-
str(str) –A hexadecimal string representing the node's unique ID.
Examples:
>>> node = BiDirection("A")
>>> isinstance(node.id, str)
True
insert_after
insert_after(node: _BiDirectionT) -> None
Insert a node immediately after the current node.
Adjusts pointers so that the new node is placed between the current node and its next node.
Parameters:
-
node(BiDirection) –The node to be inserted after the current node.
Examples:
>>> node1 = BiDirection("A")
>>> node2 = BiDirection("B")
>>> node1.insert_after(node2)
>>> node1.next is node2
True
>>> node2.prev is node1
True
Source code in ures/data_structure/bi_directional_links.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
insert_before
insert_before(node: _BiDirectionT) -> None
Insert a node immediately before the current node.
Adjusts pointers so that the new node is placed between the current node's previous node and the current node.
Parameters:
-
node(BiDirection) –The node to be inserted before the current node.
Examples:
>>> node1 = BiDirection("A")
>>> node2 = BiDirection("B")
>>> node1.insert_before(node2)
>>> node1.prev is node2
True
>>> node2.next is node1
True
Source code in ures/data_structure/bi_directional_links.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
remove
remove() -> None
Remove the current node from the linked structure.
Adjusts the previous and next nodes to bypass the current node and resets the current node's pointers to point to itself.
Examples:
>>> node1 = BiDirection("A")
>>> node2 = BiDirection("B")
>>> node1.insert_after(node2)
>>> node2.remove()
>>> node1.next is node1
True
Source code in ures/data_structure/bi_directional_links.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
search
search(value: Any) -> Optional[_BiDirectionT]
Search for a node with the specified value in the linked structure.
Starting from the current node, traverse through the chain until the value is found or the search returns to the starting node.
Parameters:
-
value(Any) –The value to search for.
Returns:
-
Optional[_BiDirectionT]–Optional[BiDirection]: The node with the matching value, or None if not found.
Examples:
>>> node1 = BiDirection("A")
>>> node2 = BiDirection("B")
>>> node3 = BiDirection("C")
>>> node1.insert_after(node2)
>>> node2.insert_after(node3)
>>> found = node1.search("C")
>>> found.value
'C'
>>> not_found = node1.search("D")
>>> not_found is None
True
Source code in ures/data_structure/bi_directional_links.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
NonCircularBiLink
NonCircularBiLink(value: Any)
Bases: BiDirection['NonCircularBiLink']
Create a non-circular doubly linked list node.
Parameters:
-
value(Any) –The value of the node.
Source code in ures/data_structure/bi_directional_links.py
242 243 244 245 246 247 248 249 250 | |
insert_after
insert_after(node: NonCircularBiLink)
Insert a node after the current node.
Parameters:
-
node(NonCircularDoublyLinkedNode) –The node to be inserted.
Source code in ures/data_structure/bi_directional_links.py
260 261 262 263 264 265 266 267 268 269 270 271 | |
insert_before
insert_before(node: NonCircularBiLink)
Insert a node before the current node.
Parameters:
-
node(NonCircularDoublyLinkedNode) –The node to be inserted.
Source code in ures/data_structure/bi_directional_links.py
273 274 275 276 277 278 279 280 281 282 283 284 | |
remove
remove()
Remove the current node from the list.
Source code in ures/data_structure/bi_directional_links.py
286 287 288 289 290 291 292 293 | |
get_head
get_head() -> NonCircularBiLink
Get the head of the list starting from the current node.
Returns:
-
NonCircularBiLink(NonCircularBiLink) –The head node of the list.
Source code in ures/data_structure/bi_directional_links.py
295 296 297 298 299 300 301 302 303 304 | |
search
search(value: Any) -> Optional[NonCircularBiLink]
Search a node by value starting from the current node.
Parameters:
-
value(Any) –The value to be searched.
Returns:
-
Optional[NonCircularBiLink]–Optional[NonCircularDoublyLinkedNode]: The node with the value if found, else None.
Source code in ures/data_structure/bi_directional_links.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
total_nodes
total_nodes() -> int
Count the total number of nodes in the list starting from the current node.
Returns:
-
int–The number of nodes reachable from this node.
Source code in ures/data_structure/bi_directional_links.py
323 324 325 326 327 328 329 330 331 332 333 334 | |