【发布时间】:2011-08-31 00:19:51
【问题描述】:
我正在学习如何在 C 中实现链表。我了解普通链表的基础知识、如何添加值、如何打印它们等,但我一直想知道 - 是否可以添加其他结构作为链表中的值?我的意思是:
typedef struct personal_info {
char *name;
char *surname;
int phone_number;
} Info;
typedef struct llist {
Info *info;
struct llist *next;
} List;
当我这样做时,如何访问Info 结构的值?
List *l;
l = malloc(sizeof(List));
l->info->name = 'name';
l->info->surname = 'surname';
l->info->phone_number = 1234567890;
代码崩溃了,所以我肯定做错了什么。您能给我一些提示如何实现吗?
【问题讨论】:
-
您是否为节点分配了内存? (信息字段)
-
您需要为 Info* 分配内存。
标签: c data-structures struct linked-list