Linked list with multiple parent and child nodes

后端 未结 6 2129
时光说笑
时光说笑 2021-01-06 00:52

I am trying to design a program that takes in data from a file, after which it gives numbering to unique data, linked list also contains parent and child lists.

Data

6条回答
  •  攒了一身酷
    2021-01-06 01:44

    You could try to separate the data from the data structure by implementing lists of pointers to data objects:

    struct data_item {
        unsigned char data;
        unsigned char id;
        unsigned int  count;
        // Whatever other data you want.
    };
    
    struct list_node {
        struct data_item *item;
        struct list_node *next;
    }
    

    Now, as we encounter characters in the file, we insert them into a "repository" data structure. For this example I'll use a simple table, but you can use a list if you want to save space or a tree if you want to save space while maintaining fast search speeds, etc.

    data_item data_table[UCHAR_MAX + 1] = {0};
    ...
    
    unsigned char data = read_character_from_file();
    struct data_item *di = data_table[data];
    
    if (di == NULL)
        di = new_data_item(data);
    else
        ++di->count;
    

    And attach them to the current list:

    struct list_node *list;
    if (first_item_in_list())
        list = new_list(di)
    else
        list - add_list(list, di);
    

    Now you can have as many such lists as you want (even a list-of-lists if you don't know the number of lists in advance).

提交回复
热议问题