vimwiki/tech/linked_list.wiki

38 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-09-23 03:33:54 +00:00
= 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 ==
2021-11-06 19:15:01 +00:00
2021-09-23 03:33:54 +00:00
{{{
Head Tail
---------------- --------- --------- --------- ----------------
| Data | | Data | | Data | | Data | | Data |
| prev* (NULL) | <= | prev* | <= | prev* | <= | prev* | <= | prev* |
| next* | => | next* | => | next* | => | next* | => | next* (NULL) |
---------------- --------- --------- --------- ----------------
}}}
[[algorithms]]