from Hacker News

Linked lists in C, with open source ADT

by AncoraImparo on 1/20/13, 1:26 AM with 38 comments

  • by AncoraImparo on 1/20/13, 2:49 AM

    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.

  • by gosu on 1/20/13, 2:30 AM

    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/

  • by tptacek on 1/20/13, 2:10 AM

    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.

  • by chj on 1/20/13, 2:26 AM

    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.

  • by afhof on 1/20/13, 2:10 AM

    On the front page, "test.c" is listed twice. Should it be "list.c" ?
  • by categorykni on 1/20/13, 1:30 AM

    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.