开发者

How to display a linked list in a struct in C

开发者 https://www.devze.com 2022-12-29 23:38 出处:网络
typedef struct child_list {int count; char vo[100]; child_list*next;} child_list; typedef struct parent_list
typedef struct child_list {int count; char vo[100]; child_list*next;} child_list;
typedef struct parent_list
{ char vo[100];
child_list * head;
int count;
parent_list * next; } parent_list;

As you can see there are two structures. child_list is used to create a linked list. And this list will be stored in a linked list of parent list. My problem is to display the child list which in the parent_list.

My desire to get while displaying the linked list of parent_list:

This lists work with this logic. I already made append and other stuff.

For example if I enter ab cd ab ja cd ab

Word    Count    List

ab        3      cd->ja

cd        2      ab->ab

ja        1      cd

The problematic part is displaying child_list which is in the parent_list nodes(List column of output). I don't know my question is 开发者_StackOverflow社区clear please ask for further info.


If you just want to print the parent node with its child list you could do like follows

void print_node(parent_list *parent_node) {
    printf("%s\t%d\t", parent_node->vo, parent_node->count);

    child_list *child_node = parent_node->head;
    while (child_node != NULL) {
        printf("%s", child_node->vo);

        child_node = child_node->next;

        if (child_node != NULL) {
            printf("->");
        }
    }
    printf("\n");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号