首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接列表中元素中的链接列表

链接列表中元素中的链接列表
EN

Stack Overflow用户
提问于 2022-02-05 18:27:00
回答 1查看 54关注 0票数 1

我有一个程序,可以在另一个链接列表/结构中创建一个链接列表。

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

这是我的代码:

代码语言:javascript
复制
#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;
    }
};

为什么在cs50 IDE中用gcc命令编译程序会出现分段错误,而使用命令编译呢?

输出:

代码语言:javascript
复制
~/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-05 19:53:59

您有未定义的行为,因为struct Collection *p_collection;没有初始化。clang生成的代码运行正常,可能是因为p_collection在函数main()的开头有一个空值,而gcc生成的程序崩溃是因为该指针的值无效,在add_group中取消引用时会导致分段错误。

以下是修改后的版本:

代码语言:javascript
复制
#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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71000915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档