Linked List
Download Source Files
- Download and place the following in your datastructures folder:
- Download and place the following in your tests folder:
Specification
ILinkedList
Bases: ABC
The ILinkedList interface defines a set of methods that a linked list data structure
should implement. This interface is designed to be generic, allowing it to work with any data type.
Below is a detailed explanation of each method in the interface.
Source code in src/datastructures/ilinkedlist.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 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 249 250 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
back
abstractmethod
property
Returns the last item in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.back
('mouse')
Returns:
| Type | Description |
|---|---|
T
|
The last item in the list |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the list is empty |
empty
abstractmethod
property
Returns True if the list is empty, False otherwise
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.empty
True
>>> linked_list.append('dog')
>>> linked_list.empty
False
Returns:
| Type | Description |
|---|---|
bool
|
True if the list is empty, False otherwise |
front
abstractmethod
property
Returns the first item in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.front
('dog')
Returns:
| Type | Description |
|---|---|
T
|
The first item in the list |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the list is empty |
__contains__(item)
abstractmethod
Returns True if the item is in the list, False otherwise
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> 'cat' in linked_list
True
>>> 'bird' in linked_list
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to check for in the list |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the item is in the list, False otherwise |
Source code in src/datastructures/ilinkedlist.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
__eq__(other)
abstractmethod
Returns True if the list is equal to another list, False otherwise
Examples:
>>> linked_list1 = LinkedList(data_type=str)
>>> linked_list1.append('dog')
>>> linked_list1.append('cat')
>>> linked_list1.append('mouse')
>>> linked_list2 = LinkedList(data_type=str)
>>> linked_list2.append('dog')
>>> linked_list2.append('cat')
>>> linked_list2.append('mouse')
>>> linked_list1 == linked_list2
True
>>> linked_list2.append('bird')
>>> linked_list1 == linked_list2
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The other list to compare to |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the list is equal to another list, False otherwise |
Source code in src/datastructures/ilinkedlist.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
__init__(data_type=object)
abstractmethod
Initializes the LinkedList object with a data_type.
Examples:
>>> linked_list = LinkedList(data_type=int)
>>> print(linked_list)
()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_type
|
type
|
The type of the elements in the list |
object
|
Source code in src/datastructures/ilinkedlist.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
__iter__()
abstractmethod
Returns an iterator for the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> for item in linked_list:
... print(item)
dog
cat
mouse
Returns:
| Type | Description |
|---|---|
ILinkedList[T]
|
An iterator for the list |
Source code in src/datastructures/ilinkedlist.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
__len__()
abstractmethod
Returns the number of items in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> len(linked_list)
0
>>> linked_list.append('dog')
>>> len(linked_list)
1
Returns:
| Type | Description |
|---|---|
int
|
The number of items in the list |
Source code in src/datastructures/ilinkedlist.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
__next__()
abstractmethod
Returns the next item in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> iterator = iter(linked_list)
>>> next(iterator)
'dog'
>>> next(iterator)
'cat'
>>> next(iterator)
'mouse'
>>> next(iterator)
Traceback (most recent call last):
...
StopIteration
Returns:
| Type | Description |
|---|---|
T
|
The next item in the list |
Raises:
| Type | Description |
|---|---|
StopIteration
|
If there are no more items in the list |
Source code in src/datastructures/ilinkedlist.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
__repr__()
abstractmethod
Returns a string representation of the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> repr(linked_list)
'LinkedList()'
>>> linked_list.append('dog')
>>> repr(linked_list)
'LinkedList(dog) Count: 1'
>>> linked_list.append('cat')
>>> repr(linked_list)
'LinkedList(dog <-> cat) Count: 2'
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the list |
Source code in src/datastructures/ilinkedlist.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
__reversed__()
abstractmethod
Returns a reversed version of the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> reversed_list = reversed(linked_list)
>>> print(reversed_list)
(mouse <-> cat <-> dog)
Returns:
| Type | Description |
|---|---|
ILinkedList[T]
|
A reversed version of the list |
Source code in src/datastructures/ilinkedlist.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
__str__()
abstractmethod
Returns a string representation of the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> str(linked_list)
'()'
>>> linked_list.append('dog')
>>> str(linked_list)
'(dog)'
>>> linked_list.append('cat')
>>> str(linked_list)
'(dog <-> cat)'
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the list |
Source code in src/datastructures/ilinkedlist.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
append(item)
abstractmethod
Adds an item to the end of the list
Examples:
>>> linked_list = LinkedList(data_type=int)
>>> for i in range(10):
... linked_list.append(i)
>>> print(linked_list)
(0 <-> 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> 7 <-> 8 <-> 9)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to add to the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item is not of the correct type |
Source code in src/datastructures/ilinkedlist.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
clear()
abstractmethod
Removes all items from the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.clear()
>>> print(linked_list)
()
Source code in src/datastructures/ilinkedlist.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
from_sequence(sequence, data_type=object)
abstractmethod
staticmethod
Creates a LinkedList object from a Python list.
Examples:
>>> linked_list = LinkedList.from_sequence([1, 2, 3, 4, 5])
>>> print(linked_list)
(1 <-> 2 <-> 3 <-> 4 <-> 5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence
|
Sequence[T]
|
The sequence to create the LinkedList from |
required |
data_type
|
type
|
The data type of the items |
object
|
Returns:
| Type | Description |
|---|---|
ILinkedList[T]
|
A LinkedList object |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the sequence contains elements not of the specified data type |
Source code in src/datastructures/ilinkedlist.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
insert_after(target, item)
abstractmethod
Adds an item after the first occurrence of a target item
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.insert_after('dog', 'mouse')
>>> print(linked_list)
(dog <-> mouse <-> cat)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
T
|
The target item to insert after |
required |
item
|
T
|
The item to add to the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item or target is not of the correct type |
ValueError
|
If the target item is not in the list |
Source code in src/datastructures/ilinkedlist.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
insert_before(target, item)
abstractmethod
Adds an item before the first occurrence of a target item
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.insert_before('cat', 'mouse')
>>> print(linked_list)
(dog <-> mouse <-> cat)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
T
|
The target item to insert before |
required |
item
|
T
|
The item to add to the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item or target is not of the correct type |
ValueError
|
If the target item is not in the list |
Source code in src/datastructures/ilinkedlist.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
pop()
abstractmethod
Removes and returns the last item in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.pop()
'mouse'
>>> print(linked_list)
(dog <-> cat)
Returns:
| Type | Description |
|---|---|
T
|
The last item in the list |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the list is empty |
Source code in src/datastructures/ilinkedlist.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
pop_front()
abstractmethod
Removes and returns the first item in the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.pop_front()
'dog'
>>> print(linked_list)
(cat <-> mouse)
Returns:
| Type | Description |
|---|---|
T
|
The first item in the list |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the list is empty |
Source code in src/datastructures/ilinkedlist.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
prepend(item)
abstractmethod
Adds an item to the beginning of the list
Examples:
>>> linked_list = LinkedList(data_type=int)
>>> for i in range(10):
... linked_list.prepend(i)
>>> print(linked_list)
(9 <-> 8 <-> 7 <-> 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1 <-> 0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to add to the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item is not of the correct type |
Source code in src/datastructures/ilinkedlist.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
remove(item)
abstractmethod
Removes the first occurrence of an item from the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.remove('cat')
>>> print(linked_list)
(dog <-> mouse)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to remove from the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item is not of the correct type |
ValueError
|
If the item is not in the list |
Source code in src/datastructures/ilinkedlist.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
remove_all(item)
abstractmethod
Removes all occurrences of an item from the list
Examples:
>>> linked_list = LinkedList(data_type=str)
>>> linked_list.append('dog')
>>> linked_list.append('cat')
>>> linked_list.append('mouse')
>>> linked_list.append('cat')
>>> linked_list.remove_all('cat')
>>> print(linked_list)
(dog <-> mouse)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
T
|
The item to remove from the list |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the item is not of the correct type |
Source code in src/datastructures/ilinkedlist.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |