新手求问!关于链表的!
#include <stdio.h>#include <stdlib.h>
typedef struct node
{
char *name;
int first_class;
int second_class;
int third_class;
int fourth_class;
struct node *next;
}Node, *Linklist;
void creatlist(Linklist head, int n);
void printlist(Linklist head);
Linklist head = NULL;
int main()
{
int n;
Node *head;
head = NULL;
creatlist(head, n);
printlist(head);
return 0;
}
void creatlist(Linklist head, int n) //创建成绩列表
{
int i;
Linklist p;
head = (Linklist)malloc(sizeof(Node));
head->next = NULL;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = n; i > 0; --i) {
p = (Linklist)malloc(sizeof(Node));
printf("请输入姓名:");
scanf("%s", p->name);
printf("p->name=%s\n", p->name); //-------此处出错--------------------
printf("请输入四门分数:\n");
printf("第一:");
scanf("%d", &p->first_class);
//printf("p->first_class=%d\n", p->first_class);
printf("第二:");
scanf("%d", &p->second_class);
printf("第三:");
scanf("%d", &p->third_class);
printf("第四:");
scanf("%d", &p->fourth_class);
p->next = head->next;
head->next = p;
}
}
void printlist(Linklist head) //打印成绩列表
{
Linklist p = head;
p = (struct node *)malloc(sizeof(struct node));
if (p != NULL) {
printf("ok\n");
}else {
printf("fail\n");
}
printf("显示所有的元素:");
while (p) {
printf("%s, %d, %d, %d, %d", p->name, p->first_class, p->second_class, p->third_class, p->fourth_class);
p = p->next;
}
printf("\n");
}