28 lines
1.4 KiB
Markdown
28 lines
1.4 KiB
Markdown
|
The first basic data-structure everybody should learn is the singly linked list.
|
||
|
|
||
|
In a singly linked list you save your data in nodes, a combination of data and a pointer
|
||
|
to the next node. To add nodes you simply allocate them and rewire the pointers,
|
||
|
such that it appears in the right spot in the list.
|
||
|
|
||
|
To remove an element you have to rewire the previous and next node, such that their
|
||
|
pointers point to each other, and then free the memory.
|
||
|
|
||
|
The idea behind this data-structures is, that you can add and remove elements easily
|
||
|
from the end, front and middle of the list. And because you just allocate more memory,
|
||
|
the list can grow dynamically.
|
||
|
|
||
|
The problem is to get to the elements.
|
||
|
Because they are allocated on the heap the elements are all over the place and can only
|
||
|
be found via the pointers. So, when you want to get to the middle, you have to traverse
|
||
|
the linked list.
|
||
|
|
||
|
Another downside of singly linked list is, that you can't go backwards.
|
||
|
That is were the doubly linked list comes in, but more on that later.
|
||
|
|
||
|
The last disadvantage is, when you want to process lot's of data in a short amount of time,
|
||
|
you are often limited by cache misses. And linked list in general are terrible for that kind
|
||
|
of work, because most elements aren't in the same cache line.
|
||
|
|
||
|
Your task will be to implement the functions for add, insert_after, remove and get.
|
||
|
It is often helpful to draw the operations you want to do on paper or
|
||
|
look at some pictures.
|