I was thinking whether there is another type of declaration other than self referential structures in C for implementing linked list.
No, linked lists can only be implemented by using an explicit or implicit pointer to their own type.
Masking this self reference using void*
pointers is possible, but it only complicates the code, making the self reference implicit and making it more difficult to read the code.
Arrays:
Sure, you could use an unordered array and make it "ordered" by an internal indexing scheme that looks like a linked list - but it still be an array and it will not have the same behavior as a classical linked list.
For example, your array based linked list may be bounded to a pre-selected number of elements (selected during compilation), causing higher memory consumption when the list isn't full and making it impossible to add elements after a certain point.
On the flip side, if your array based linked list is dynamic, item copying during realloc
may introduce performance costs (for example, making an O(1) operation perform at O(n)).
Another thing to think about is that a linked list based on a dynamic array will cause pointers to the data to be invalidated every time the array changes its address (realloc
).