38 lines
1.2 KiB
Plaintext
38 lines
1.2 KiB
Plaintext
= Linked lists =
|
|
|
|
A Sequential "list" of elements that is basically a daisy chain of pointers.
|
|
Comes in single and double linked flavors. This thing really has one good
|
|
property but that can be abused by linking it with other data structures (think
|
|
LRUCache)
|
|
|
|
== Good for ==
|
|
|
|
* Data that is often swaped or deleted, done in O(1)
|
|
|
|
== Bad for ==
|
|
|
|
* Basically everything else
|
|
|
|
|
|
== Optimize ==
|
|
|
|
If memory is a real concern you can XOR the pointers to the next and prev items.
|
|
To do this you need to xor the prev and next addresses, then when traversing
|
|
xor the previous and the combined pointer. This really is only when youre
|
|
worrying about like 4 bytes per node and either have tons of nodes or memory is
|
|
precious and the time spent XORing is a valid tradeoff.
|
|
|
|
== Visual ==
|
|
|
|
{{{
|
|
Head Tail
|
|
---------------- --------- --------- --------- ----------------
|
|
| Data | | Data | | Data | | Data | | Data |
|
|
| prev* (NULL) | <= | prev* | <= | prev* | <= | prev* | <= | prev* |
|
|
| next* | => | next* | => | next* | => | next* | => | next* (NULL) |
|
|
---------------- --------- --------- --------- ----------------
|
|
}}}
|
|
|
|
|
|
[[algorithms]]
|