Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:06:53+00:00 2026-05-18T09:06:53+00:00

I know generally two ways to design a generic linked list datastructure in C.

  • 0

I know generally two ways to design a generic linked list datastructure in C. And I’m wondering which is better. Before asking the question, I’ll introduce both methods shortly:

One method is to build functions around a structure like the following:

struct list_element {
    struct list_element *prev;
    struct list_element *next;
    void *data;
};

Obviously, the data pointer points to the payload. The list element struct is outside the payload. This is e.g. how glib has designed its double linked list facility: http://library.gnome.org/devel/glib/2.26/glib-Doubly-Linked-Lists.html

Another method is the way how it’s done in the Linux kernel: http://isis.poly.edu/kulesh/stuff/src/klist/. There is no void pointer to the payload in the list element struct. Instead the list element struct is included in the payload struct:

struct list_element {
    struct list_element *prev;
    struct list_element *next;
};

struct person {
    char name[20];
    unsigned int age;
    struct list_element list_entry;
};

A special macro is used to get a pointer to the payload struct given a pointer to the list_entry, its name withing the payload struct and the type of the payload struct (the list_entry() macro).

Finally, here is the question: What is the advantage of the latter of the two methods of constructing a linked list? A few times I’ve heard people say the second is more ‘generic’ than the first, but why? I would even argue that the first method is more generic because the payload structures are list implementation agnostic, which isn’t the case with the second method.
One more downside of the second method is if you want to place the payload in more than one list, you should a struct list_element member for each list in the payload structure.

Edit:
To summarize so far I saw two answers which were important for me:

  • With the first method: removing a payload from the list involves looping through the complete list until the list element pointing to the payload is found. You don’t need to do this with the second method. (Answer from Patrick)
  • With the first method you have to do two malloc()s for each element: one for the payload and one for the list element struct. With the second method one malloc() is sufficient. (Answer from Roddy)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:06:53+00:00Added an answer on May 18, 2026 at 9:06 am

    The first approach might seem less intrusive but in many cases it isn’t (unless you add additional data structures).

    Imagine you have a list of thousand persons and you want to remove one of them from the list. If the person doesn’t know where it is in the list, you will have to scan the whole list first to get the exact place of the person.

    You can solve this by adding a pointer from the person to its corresponding list structure, but this defeats the non-intrusiveness (does this word exist?) of the solution.

    Another alternative is to have a hash map that maps the memory addresses of the persons to the memory addresses of the list nodes. Then finding the node in the list is much faster (but still slower than the intrusive way). However, since this will take a even more memory, I suggest not to do this.

    Therefore, the easiest and simplest solution is the second one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know how to generally move around in command mode, specifically, jumping to lines,
I know it's generally a bad idea to do queries like this: SELECT *
I've seen this is various codebases, and wanted to know if this generally frowned
I am reading a book that compares two ways of implementing threads, Middleware Threads
Prior to asking this question here, I have googled aroung. In general, people suggest
I generaly disable viewstate for my ASP.net controls unless I explicitly know I am
I would like to know if there are general rules for creating an index
Anyone know of a good way to unit test Scala actors? In the general
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Know of any good libraries for this? I did some searches and didn't come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.