[英]Linked list in an element in linked list
I have a program to make a linked list in another linked list\/struct.我有一个程序可以在另一个链表\/结构中创建一个链表。
I have a
struct Collection<\/code> which contains a pointer to the head of a linked list
Group<\/code> .
我有一个
struct Collection<\/code> ,其中包含一个指向链表
Group<\/code>头部的指针。
The function
add_group<\/code> , adds another node to the
Group<\/code> linked list.
函数
add_group<\/code>将另一个节点添加到
Group<\/code>链表中。
Finally I print my linked list using the
print_collection<\/code> function.
最后,我使用
print_collection<\/code>函数打印我的链表。
Here is my code:这是我的代码:
#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;
}
};
You have undefined behavior because struct Collection *p_collection;<\/code>
你有未定义的行为,因为
struct Collection *p_collection;<\/code>
is not initialized.
未初始化。 The code generated by clang<\/strong> behaves as expected, possibly because
p_collection<\/code> happens to have a null value at the beginning of the function
main()<\/code> , whereas the program generated by gcc<\/strong> crashes because this pointer has an invalid value that causes a segmentation fault when dereferenced in
add_group<\/code> .
clang<\/strong>生成的代码的行为符合预期,可能是因为
p_collection<\/code>在函数
main()<\/code>的开头碰巧有一个空值,而gcc<\/strong>生成的程序崩溃是因为这个指针有一个无效的值,在取消引用时会导致分段错误
add_group<\/code> 。
Here is a modified version:这是修改后的版本:
#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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.