Hacker News new | ask | show | jobs
by yason 4771 days ago
Who the heck puts the payload into a separate allocation?

I'm pretty sure I've never seen such an implementation. Not once. I could imagine seeing something like that in textbooks where they use graphical diagrams to illustrate how a linked list works but who would actually implement it like that -- I don't know.

The canonical way is to do:

  struct listnode {
    struct listnode *next;
    struct listnode *prev;
  };

  struct your_own_data_node {
    struct listnode node;
    int x, y, z;
  };
which ensures that you can have a set of functions that operate on struct listnode * and you can use them for all of your lists.
1 comments

I wouldn't call that canonical. You can take a further step, as in the Linux version that someone posted below:

http://kernelnewbies.org/FAQ/LinkedLists