链表中元素中的链表

Linked list in an element in linked list

我有一个程序可以在另一个链接中创建一个链表 list/struct。

我有一个 struct Collection,它包含一个指向链表头部的指针 Group。函数add_group,将另一个节点添加到Group 链表中。最后,我使用 print_collection 函数打印我的链表。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

struct Group {
    int id;
    int grp_count;
    struct Group *next;
};

struct Collection {
    int total_count;
    struct Group *grp_head;
    struct Collection *next;
};

void add_group(int grp_id, int grp_count, struct Collection **);
void print_collection(struct Collection *);

int main() {
    struct Collection *p_collection;
    //adding grp with id as 1 and grp_count 5
    add_group(1, 5, &p_collection);
    add_group(2, 7, &p_collection);
    add_group(3, 4, &p_collection);
    print_collection(p_collection);
}

void add_group(int grp_id, int grp_count, struct Collection **addr_p_collection) {
    //making new group
    struct Group *newGroup;
    newGroup = (struct Group *)malloc(sizeof(struct Group));
    newGroup->id = grp_id;
    newGroup->grp_count = grp_count;
    newGroup->next = NULL;

    //adding grp to collection

    //making new Collection if it doesn't exist
    if (*addr_p_collection == NULL) {
        *addr_p_collection = (struct Collection *)malloc(sizeof(struct Collection));
        (*addr_p_collection)->total_count = grp_count;
        (*addr_p_collection)->grp_head = newGroup;
        (*addr_p_collection)->next = NULL;
        return;
    } else {
        (*addr_p_collection)->total_count += grp_count;
        struct Group * tempGroup = (*addr_p_collection)->grp_head;
        while (tempGroup->next != NULL) {
            tempGroup = tempGroup->next;
        }
        tempGroup->next = newGroup;
    }
};

void print_groups(struct Group *groups_list) {
    struct Group *temp = groups_list;
    while (temp != NULL) {
        printf("Id: %d\tGroup Count: %d\n", temp->id, temp->grp_count);
        temp = temp->next;
    }
    printf("\n");
};

void print_collection(struct Collection * p_collection){
    struct Collection *temp = p_collection;
    while (temp != NULL) {
        printf("Total: %d\n", temp->total_count);
        print_groups(temp->grp_head);
        temp = temp->next;
    }
};

为什么用 gcc 命令在 cs50 IDE 中编译此程序显示分段错误,但使用 make 命令编译?

输出:

~/sem2/assign-2/ $ make d
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow d.c -lcrypt -lcs50 -lm -o d
~/sem2/assign-2/ $ ./d
Total: 16
Id: 1   Group Count: 5
Id: 2   Group Count: 7
Id: 3   Group Count: 4

~/sem2/assign-2/ $ gcc d.c
~/sem2/assign-2/ $ ./a.out
Segmentation fault

您有未定义的行为,因为 struct Collection *p_collection; 未初始化。 clang 生成的代码的行为符合预期,可能是因为 p_collection 恰好在函数 main() 的开头有一个空值,而 main() 生成的程序=19=]gcc 崩溃,因为此指针具有无效值,在 add_group.

中取消引用时会导致分段错误

这是修改后的版本:

#include <stdio.h>
#include <stdlib.h>

struct Group {
    int id;
    int grp_count;
    struct Group *next;
};

struct Collection {
    int total_count;
    struct Group *grp_head;
    struct Collection *next;
};

void add_group(int grp_id, int grp_count, struct Collection **);
void print_collection(const struct Collection *);

int main() {
    struct Collection *p_collection = NULL;

    //adding grp with id as 1 and grp_count 5
    add_group(1, 5, &p_collection);
    add_group(2, 7, &p_collection);
    add_group(3, 4, &p_collection);
    print_collection(p_collection);
    return 0;
}

void add_group(int grp_id, int grp_count, struct Collection **addr_p_collection) {
    //making new group
    struct Group *newGroup;
    newGroup = (struct Group *)malloc(sizeof(struct Group));
    newGroup->id = grp_id;
    newGroup->grp_count = grp_count;
    newGroup->next = NULL;

    //adding grp to collection

    struct Collection *p_collection = *addr_p_collection;
    //making new Collection if it doesn't exist
    if (p_collection == NULL) {
        *addr_p_collection = p_collection = (struct Collection *)malloc(sizeof(struct Collection));
        p_collection->total_count = grp_count;
        p_collection->grp_head = newGroup;
        p_collection->next = NULL;
    } else {
        p_collection->total_count += grp_count;
        if (p_collection->grp_head == NULL) {
            p_collection->grp_head = newGroup;
        } else {
            struct Group *tempGroup = p_collection->grp_head;
            while (tempGroup->next != NULL) {
                tempGroup = tempGroup->next;
            }
            tempGroup->next = newGroup;
        }
    }
}

void print_groups(const struct Group *groups_list) {
    const struct Group *temp = groups_list;
    while (temp != NULL) {
        printf("Id: %d\tGroup Count: %d\n", temp->id, temp->grp_count);
        temp = temp->next;
    }
    printf("\n");
}

void print_collection(const struct Collection *p_collection) {
    const struct Collection *temp = p_collection;
    while (temp != NULL) {
        printf("Total: %d\n", temp->total_count);
        print_groups(temp->grp_head);
        temp = temp->next;
    }
}