Is it possible to have a linked list of different data types?

后端 未结 11 2065
独厮守ぢ
独厮守ぢ 2020-12-13 22:22

This is just another interview question.

Can we have a linked list of different data types, i.e. each element in a linked list can have different structure or union

相关标签:
11条回答
  • 2020-12-13 23:11

    Yes, I do this by defining the list's element's value as a void pointer void*. In order to know the type stored in each element of the list I also have a .type field in there, so I know how to dereference what the pointer is pointing to for each element.

    struct node {
        struct node* next;
        int type;
        void* value;
    };
    

    Here's a full example of this:

    //                                                                                                                                                                                          
    // An exercise to play with a struct that stores anything using a void* field.                                                                                                              
    //                                                                                                                                                                                          
    
    #include <stdio.h>
    
    #define TRUE 1
    
    int TYPE_INT = 0;
    int TYPE_STRING = 1;
    int TYPE_BOOLEAN = 2;
    int TYPE_PERSON = 3;
    
    struct node {
      struct node* next;
      int type;
      void* value;
    };
    
    struct person {
      char* name;
      int age;
    };
    
    int main(int args, char **argv) {
    
      struct person aPerson;
      aPerson.name = "Angel";
      aPerson.age = 35;
    
      // Define a linked list of objects.                                                                                                                                                       
      // We use that .type field to know what we're dealing                                                                                                                                     
      // with on every iteration. On .value we store our values.                                                                                                                                
      struct node nodes[] = {
        { .next = &nodes[1], .type = TYPE_INT    , .value=1                   },
        { .next = &nodes[2], .type = TYPE_STRING , .value="anyfing, anyfing!" },
        { .next = &nodes[3], .type = TYPE_PERSON , .value=&aPerson            },
        { .next = NULL     , .type = TYPE_BOOLEAN, .value=TRUE                }
      };
    
      // We iterate through the list                                                                                                                                                            
      for ( struct node *currentNode = &nodes[0]; currentNode;  currentNode = currentNode->next) {
        int currentType = (*currentNode).type;
        if (currentType == TYPE_INT) {
          printf("%s: %d\n", "- INTEGER", (*currentNode).value); // just playing with syntax, same as currentNode->value                                                                        
        } else if (currentType == TYPE_STRING) {
          printf("%s: %s\n", "- STRING", currentNode->value);
        } else if (currentType == TYPE_BOOLEAN) {
          printf("%s: %d\n", "- BOOLEAN (true:1, false:0)", currentNode->value);
        } else if (currentType == TYPE_PERSON) {
            // since we're using void*, we end up with a pointer to struct person, which we *dereference                                                                                        
            // into a struct in the stack.                                                                                                                                                      
            struct person currentPerson = *(struct person*) currentNode->value;
            printf("%s: %s (%d)\n","- TYPE_PERSON", currentPerson.name, currentPerson.age);
          }
      }
    
        return 0;
    }
    

    Expected output:

    - INTEGER: 1
    - STRING: anyfing, anyfing!
    - TYPE_PERSON: Angel (35)
    - BOOLEAN (true:1, false:0): 1
    
    0 讨论(0)
  • 2020-12-13 23:12

    As said, you can have a node this questionwith a void*. I suggest using something to know about your type :

    typedef struct
    {
        /* linked list stuff here */    
    
        char m_type;
        void* m_data;
    } 
    Node;
    

    See this question.

    0 讨论(0)
  • 2020-12-13 23:14

    If you don't want to have to specify the type of every node in the list via the union solution you can always just store the data in a char* and take type-specific function pointers as parameters to type-sensitive operations such as printing or sorting the list. This way you don't have to worry about what node is what type and can just cast the data however you like.

    /* data types */
    
    typedef struct list_node list_node;
    struct list_node {
        char *data;
        list_node *next;
        list_node *prev;
    };
    
    typedef struct list list;
    struct list {
        list_node *head;
        list_node *tail;
        size_t size;
    };
    
    /* type sensitive functions */
    
    int list_sort(list *l, int (*compar)(const void*, const void*));
    int list_print(list *l, void (*print)(char *data));
    
    0 讨论(0)
  • 2020-12-13 23:19

    You can have each node in a linked list have a void* that points to your data. It's up to you how you determine what type of data that pointer is pointing to.

    0 讨论(0)
  • 2020-12-13 23:19

    I use these macros I wrote to make general linked lists. You just create your own struct and use the macro list_link somewhere as a member of the struct. Give that macro one argument naming the struct (without the struct keyword). This implements a doubly linked list without a dummy node (e.g. last node links back around to first node). The anchor is a pointer to the first node which starts out initialized by list_init(anchor) by giving it the lvalue (a dereferenced pointer to it is an lvalue). Then you can use the other macros in the header. Read the source for comments about each available macro functions. This is implemented 100% in macros.

    http://phil.ipal.org/pre-release/list-0.0.5.tar.bz2

    0 讨论(0)
提交回复
热议问题