Linked lists in C, with open source ADT

  • Sorry for posting this, it appears that HN is not the place to post content if your only goal is to help people to learn. The hacker ethic is dead I guess, Richard Stallman really was the last man standing then. Remind me to come and post something when I decide to make some silly iPhone / android app when I want to make money rather than sharing knowledge.

    Thanks.

  • This isn't how I'd do a linked list in C. Having to malloc separate storage for the next/prev pointers imposes a big cost in terms of performance (it's hardly O(1)) and robustness. Your linked list operations can all fail now - sooner or later, that's going to put you in a very tight spot.

    I prefer to store next/prev pointers with the data itself, and to use offsetof/containerof to find a pointer to the data struct which contains a given set of next/prev pointers.

    One example of such an scheme is the list used in the Linux kernel:

    http://lxr.linux.no/#linux+v3.7.3/include/linux/list.h

    http://lwn.net/Articles/336255/

  • The book _C Interfaces and Implementation_ is nothing but library interfaces like this and their implementations.

    You are, for whatever it's worth, better off defaulting to a variable-length array than to a linked list.

  • Some simple suggestions:

    1. Please align the asterisks properly. It hurts eyes.

    2. Access by index?

    3. Iterator (callback on each element)

    4. use 'init' instead of 'initialize'. Extra typing hurts fingers.

  • On the front page, "test.c" is listed twice. Should it be "list.c" ?

  • This is week-one stuff of a CS degree or on the job training. If you don't know this you shouldn't be programming.