Data Structure - Bi-Directional Links
BiDirection
Bases: Generic[_BiDirectionT]
Source code in ures/data_structure/bi_directional_links.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
id
property
Get the unique identifier of the node.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A hexadecimal string representing the node's unique ID. |
Example
node = BiDirection("A") isinstance(node.id, str) True
next
property
Get the next node in the linked structure.
Returns:
| Name | Type | Description |
|---|---|---|
BiDirection |
_BiDirectionT
|
The next node. |
Example
node = BiDirection("A") node.next is node True
prev
property
Get the previous node in the linked structure.
Returns:
| Name | Type | Description |
|---|---|---|
BiDirection |
_BiDirectionT
|
The previous node. |
Example
node = BiDirection("A") node.prev is node True
value
property
Retrieve the value stored in the node.
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The node's value. |
Example
node = BiDirection(123) node.value 123
__eq__(other)
Check equality between two nodes based on their unique IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
BiDirection
|
Another node to compare with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if both nodes have the same unique ID, False otherwise. |
Example
node1 = BiDirection("A") node2 = BiDirection("A") node1 == node1 True node1 == node2 False
Source code in ures/data_structure/bi_directional_links.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
__init__(value)
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:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to store in the node. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Example
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 32 33 34 | |
__repr__()
Get a detailed string representation of the node.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string in the format "BiDirection( |
Example
node = BiDirection("World") repr(node) 'BiDirection(World)'
Source code in ures/data_structure/bi_directional_links.py
236 237 238 239 240 241 242 243 244 245 246 247 248 | |
__str__()
Get the string representation of the node.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representing the node's value. |
Example
node = BiDirection("Hello") str(node) 'Hello'
Source code in ures/data_structure/bi_directional_links.py
222 223 224 225 226 227 228 229 230 231 232 233 234 | |
insert_after(node)
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:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
BiDirection
|
The node to be inserted after the current node. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
insert_before(node)
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:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
BiDirection
|
The node to be inserted before the current node. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
remove()
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.
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
search(value)
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:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to search for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[_BiDirectionT]
|
Optional[BiDirection]: The node with the matching value, or None if not found. |
Example
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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
NonCircularBiLink
Bases: BiDirection['NonCircularBiLink']
Source code in ures/data_structure/bi_directional_links.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
__init__(value)
Create a non-circular doubly linked list node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value of the node. |
required |
Source code in ures/data_structure/bi_directional_links.py
254 255 256 257 258 259 260 261 262 | |
get_head()
Get the head of the list starting from the current node.
Returns:
| Name | Type | Description |
|---|---|---|
NonCircularBiLink |
NonCircularBiLink
|
The head node of the list. |
Source code in ures/data_structure/bi_directional_links.py
315 316 317 318 319 320 321 322 323 324 | |
insert_after(node)
Insert a node after the current node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NonCircularDoublyLinkedNode
|
The node to be inserted. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in ures/data_structure/bi_directional_links.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
insert_before(node)
Insert a node before the current node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
NonCircularDoublyLinkedNode
|
The node to be inserted. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in ures/data_structure/bi_directional_links.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
remove()
Remove the current node from the list.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in ures/data_structure/bi_directional_links.py
302 303 304 305 306 307 308 309 310 311 312 313 | |
search(value)
Search a node by value starting from the current node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to be searched. |
required |
Returns:
| Type | Description |
|---|---|
Optional[NonCircularBiLink]
|
Optional[NonCircularDoublyLinkedNode]: The node with the value if found, else None. |
Source code in ures/data_structure/bi_directional_links.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
total_nodes()
Count the total number of nodes in the list starting from the current node.
Returns:
| Name | Type | Description |
|---|---|---|
int |
The total number of nodes in the list. |
Source code in ures/data_structure/bi_directional_links.py
343 344 345 346 347 348 349 350 351 352 353 354 | |