I need a double linked list in C, but it must be for different types. In C++ we use templates for it.
Where can I find an example in C for double linked list with abstract data type items?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few approaches you can take, one of which involves storing a
void*in your ADT.I’ve always found this to be a bit of a pain in a linked list since you have to manage it’s allocation separately to the list itself. In other words, to allocate a node, you need to alocate both the node and its payload separately (and remember to clean them both up on deletion as well).
One approach I’ve used in the past is to have a ‘variable sized’ structure like:
Now that doesn’t look variable sized but let’s allocate a structure thus:
Now you have a node that, for all intents and purposes, looks like this:
or, in graphical form (where
[n]meansnbytes):That is, assuming you know how to address the payload correctly. This can be done as follows:
That cast line simply casts the address of the
payloadcharacter (in thetNodetype) to be an address of the actualtPersonpayload type.Using this method, you can carry any payload type you want in a node, even different payload types in each node, if you make the structure more like:
and use
payloadTypeto store an indicator as to what the payload actually is.This has the advantage over a union in that it doesn’t waste space, as can be seen with the following:
where 96 bytes are wasted every time you store an integer type in the list (for a 4-byte integer).
The payload type in the
tNodeallows you to easily detect what type of payload this node is carrying, so your code can decide how to process it. You can use something along the lines of:or (probably better):
The only thing you need to watch out for is to ensure that the alignment of the payload is correct. Since both my payload placeholder and the payload are all
chartypes, that’s not an issue. However, if your payload consists of types with more stringent alignment requirements (such as something more strict than the pointers, you may need to adjust for it).While I’ve never seen an environment with alignments more strict than pointers, it is possible according to the ISO C standard.
You can usually get the required alignment simply by using a data type for the payload placeholder which has the strictest alignment requirement such as:
In retrospect, it occurs to me that you probably don’t need an array as the payload placeholder. It’s simple enough to just have something you can take the address of. I suspect that particular idiom of mine hearkens back to the days where I just stored an array of characters (rather than a structure) and referenced them directly. In that case, you could use
payload[]on its own without casting to another type.