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 938433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:38:36+00:00 2026-05-15T21:38:36+00:00

I need a double linked list in C, but it must be for different

  • 0

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?

  • 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-15T21:38:37+00:00Added an answer on May 15, 2026 at 9:38 pm

    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:

    typedef struct _tNode {
        struct _tNode *prev;
        struct _tNode *next;
        char payload[1];
    } tNode;
    

    Now that doesn’t look variable sized but let’s allocate a structure thus:

    typedef struct {
        char Name[30];
        char Addr[50];
        char Phone[20];
    } tPerson;
    tNode *node = malloc (sizeof (tNode) - 1 + sizeof (tPerson));
    

    Now you have a node that, for all intents and purposes, looks like this:

    typedef struct _tNode {
        struct _tNode *prev;
        struct _tNode *next;
        char Name[30];
        char Addr[50];
        char Phone[20];
    } tNode;
    

    or, in graphical form (where [n] means n bytes):

    +------------+
    | prev[4]    |
    +------------+
    | next[4]    |
    +------------+ +-----------+
    | payload[1] | | Name[30]  | <- overlap
    +------------+ +-----------+
                   | Addr[50]  |
                   +-----------+
                   | Phone[20] |
                   +-----------+
    

    That is, assuming you know how to address the payload correctly. This can be done as follows:

    node->prev = NULL;
    node->next = NULL;
    tPerson *person = &(node->payload); // cast for easy changes to payload.
    strcpy (person->Name, "Richard Cranium");
    strcpy (person->Addr, "10 Smith St");
    strcpy (person->Phone, "555-5555");
    

    That cast line simply casts the address of the payload character (in the tNode type) to be an address of the actual tPerson payload 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:

    typedef struct _tNode {
        struct _tNode *prev;
        struct _tNode *next;
        int payloadType;       // Allows different payload type at each node.
        char payload[1];
    } tNode;
    

    and use payloadType to 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:

    union {
        int fourBytes;
        char oneHundredBytes[100];
    } u;
    

    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 tNode allows 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:

    #define PAYLOAD_UNKNOWN     0
    #define PAYLOAD_MANAGER     1
    #define PAYLOAD_EMPLOYEE    2
    #define PAYLOAD_CONTRACTOR  3
    

    or (probably better):

    typedef enum {
        PAYLOAD_UNKNOWN,
        PAYLOAD_MANAGER,
        PAYLOAD_EMPLOYEE,
        PAYLOAD_CONTRACTOR
    } tPayLoad;
    

    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 char types, 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:

    long payload;
    

    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.

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

Sidebar

Related Questions

i have a double linked list with many list elements. so i can use
I need to use a double linked list using PHP for my script so
I am looking for book on data structure which guides in linked list, double
I need to wrap a dynamically allocated array(from a = new double[100] for example)
I need to merge two doubly-linked lists, but not by their values (the lists
Hello I have a double linked list set up, and i have a search
I need you to review my implementation of a Singly Linked List (SLL) please.
I need to be able to do the following: search a linked list. add
Do we need to do double buffering in BlackBerry while rendering our paint code
I have been developing the application for drawing and I need to detect double

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.