In the last C project I needed a versatile list data structure.
The needs for this list where:
- lightweight on memory
- easy to use
- support for every kind of information I wanted to store
Given those premises, I decided to create a classic list data structure that instead of using the correct data type, it uses (void *) as the information holder.
Using a void pointer gives you a lot of flexibility because in this way you can store every kind of information you need. You can even obtain a list that holds different element types!
However the use of (void *) requires a bit of a consideration. In fact, deleting a node from the list becomes difficult because the information that is being held by the list node needs an appropriate deallocator function. Another reason of concern is that when you get the info from a node list, you have to cast it as the element type that you know it is.
Continue reading “Lightweight and multipurpose list data structure in C”