gpt4 book ai didi

c - 添加/更新包含在另一个链表中的链表中的信息(C 编程)

转载 作者:行者123 更新时间:2023-11-30 16:38:22 25 4
gpt4 key购买 nike

我是一个初学者,试图向另一个链接列表中包含的链接列表添加信息/更新信息。我的程序是一个简单的联系人列表,其中用户可以将联系人添加到列表中,然后向每个联系人添加信息(使用单独的信息列表)。我的 addInformation 函数出现段错误,但我看不出问题是什么。以下是结构的定义方式:

typedef struct info {
char *name;
char *value;
struct info *next;
} Info;

typedef struct contact {
char *name;
Info *information;
struct contact *next;
} Contact;

这是我的 addInformation 函数:

void addInformation(Contact *myContacts, char *contactName, char *infoName, char *infoValue) {
Contact *ptr = myContacts;
Info *ptr2 = ptr->information;

if (ptr == NULL) {
printf("Error: No contacts added.\n");
return;
}

while (ptr != NULL) {
if (ptr->name != contactName) {
printf("Error: Contact does not exist\n");
return;
}
else {
ptr2->name = infoName;
ptr2->value = infoValue;
ptr2->next = NULL;
ptr->information = ptr2;
}
ptr = ptr->next;
}

return;
}

如果有人能告诉我我的代码中做错了什么,我将不胜感激!谢谢。

最佳答案

我的第一 react 是联系人->信息的内存没有分配。因此 ptr2 是 NULL 或垃圾。 @yano 已经提到过。

我尝试在原始代码上写注释,然后决定编写带有一些注释的新版本会更清晰。如果将代码拆分成多个函数就更好了。

void addInformation(Contact *myContacts, char *contactName, char *infoName, char *infoValue) {
Contact *ptr = myContacts;

if (ptr == NULL) {
printf("Error: bad pointer to contacts storage.\n");
return;
}

while (ptr != NULL) {
if (strcmp(ptr->name,contactName) == 0) {
// if it's a contact with the name we are looking for
// then allocate and put info structure as a head of the list of info
Info *newInfo = malloc(sizeof(Info));
newInfo->name = infoName;
newInfo->value = infoValue;
newInfo->next = ptr->information;
ptr->information = newInfo;
return; // get out as we found our contact
} else if (ptr->next == NULL ) {
// there is nothing next, we are at the end of the list
// allocate memory for both contact and information, fill it in
// add the contact as the last element in the list of contacts.
Contact* newContact = malloc(sizeof(Contact));
newContact->name = contactName;
newContact->information = malloc(sizeof(Info));
newContact->information->name = infoName;
newContact->information->value = infoValue;
newContact->information->next = NULL;
newContact->next = NULL;
ptr->next = newContact;
return; // get out as there is no reason to iterate more, it's the last element in the list
} else {
ptr = ptr->next;
}
}


return;
}

int main(int argc, const char * argv[]) {
// Has to allocate the first contact. Otherwise the pointer management
// will have to be different and addInformation function has to accept
// pointer to pointer.
Info info = { "nn", "vv", NULL };
Contact myContacts = { "contact 1", &info, NULL };

addInformation(&myContacts, "contact 2", "info 1", "val 2");
addInformation(&myContacts, "contact 2", "info 3", "val 4");
addInformation(&myContacts, "contact 1", "info 5", "val 6");

printf("end");
return 0;
}

关于c - 添加/更新包含在另一个链表中的链表中的信息(C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com