结构体的问题!!!
刚刚才学了结构体 还是有很多地方弄不清楚如果要写一段程序达到“使用结构体储存n个人的信息”的目的
要有n个结点
先读入 EOF结束
用malloc函数创建链表 那应该就是说还要用到free函数???
读入顺序
人名 性别 年龄(回车)
人名 性别 年龄(回车)
...
以此类推
在这里假设n==3
输入:
asd vd F 15(在这里asd vd是个人名)
b M 56
c F 34
要怎么写??
我写的老是崩溃 也不知道什么原因
这个问题应该花不了大神们十分钟。。。刚注册论坛分也不多
求助啊 谢谢~
代码在这
程序代码:#include<stdio.h>
#include<stdlib.h>
struct people *creat_head();
void data_in(struct people *head, struct people *p);
void data_pout(struct people *head, struct people *p);
struct people {
char *info;
struct people *next;
};
int main() {
int i;
struct people *head, *p;
head = creat_head();
p = head;
for (i = 0; i < 3; i++) {
data_in(head, p);
p->next = NULL;
}
data_pout(head, p);
free(head);
return 0;
}
struct people *creat_head() {
struct people *creathead;
creathead = (struct people*) malloc(sizeof(struct people));
if (!creathead)
return NULL ;
creathead->next = NULL;
return creathead;
}
void data_in(struct people *head, struct people *p) {
p = (struct people*) malloc(sizeof(struct people));
while (gets(p->info)) {
}
}
void data_pout(struct people *head, struct people *p) {
for (p = head; p != NULL ; p = p->next)
printf("%s\n", p->info);
}刚从数组学到链表 不懂的地方太多了
[ 本帖最后由 celisius 于 2012-12-14 10:23 编辑 ]









