#include
#include
typedef struct node
{
char name[100];
int score;
struct node *next;
}StudList;
void CreateStudent(StudList **sl)
{
int n;
StudList s, *tu;
sl = (StudList)malloc(sizeof(StudList));
tu = sl;
printf("学生人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
s = (StudList *)malloc(sizeof(StudList));
printf("第%d个学生姓名和成绩:", i + 1);
scanf("%s-%d", s->name,&s->score);
//scanf("%d", &s->score);
tu->next = s;
tu = s;
}
tu->next = NULL;
}
void DispList(StudList *L)
{
StudList *p = L->next;
printf(" 名次 姓名 成绩\n");
int i = 1;
while (p != NULL)
{
printf(" %d\t\t", i++);
printf("%s\t\t", p->name);//有错
printf("%d\n", p->score);
p = p->next;
}
}
int main()
{
StudList *n;
printf("建立学生表\n");
CreateStudent(&n);
DispList(&n);
system("pause");
return 0;
}
大神们,我每次运行的时候都提示我注释的地方有异常,谁能帮我调一下,有赏啊,兄弟们
你这代码 问题很多
DispList(&n); &n 是 StudList** 类型 而你的函数 原型 需要 StudList* 类型
scanf("%s-%d", s->name,&s->score); 好像 并不能正确的输入
sl = (StudList)malloc(sizeof(StudList)); 这类型 都不一致 不知道你怎么能编译不报错呢
*pstu = (StudList *)malloc(sizeof(StudList));
malloc开辟空间后返回指针变量,该指针指向所申请的空间地址。StudList s.你定义的s是个变量不是StudList类型的指针。
参考:http://blog.csdn.net/huangjianxiang1875/article/details/8423488
推荐你去看一下浙大翁恺老师的C语言课程,其中有链表的讲述
仅供参考
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char name[100];
int score;
struct node *next;
}StudList;
void CreateStudent(StudList **sl)
{
int n;
StudList *s, *tu;//s应该是指针
*sl = (StudList *)malloc(sizeof(StudList));//此处malloc的内存应该转换为结构体类型指针并赋值给同类型变量,而不是指针的指针
tu = *sl;
printf("学生人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
s = (StudList *)malloc(sizeof(StudList));
printf("第%d个学生姓名和成绩:", i + 1);
scanf("%s-%d", s->name,&s->score);//用-没有用,会将你输入的东西全部作为字符串保存到name中
//scanf("%d", &s->score);
tu->next = s;
tu = s;
}
tu->next = NULL;
}
void DispList(StudList *L)
{
StudList *p = L->next;
printf(" 名次 姓名 成绩\n");
int i = 1;
while (p != NULL)
{
printf(" %d\t\t", i++);
printf("%s\t\t", p->name);//有错
printf("%d\n", p->score);
p = p->next;
}
}
int main()
{
StudList *n;
printf("建立学生表\n");
CreateStudent(&n);
DispList(n);//----
system("pause");
return 0;
}
修改了部分代码,已经可以正常运行了
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
char name[100];
int score;
struct node *next;
}StudList;
void DispList(StudList *L);
void CreateStudent(StudList **sl)
{
int n;
StudList *s, *tu;//s改为*s
*sl = (StudList *)malloc(sizeof(StudList));//需修改
tu = *sl;//需修改
printf("学生人数:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
s = (StudList*)malloc(sizeof(StudList));
printf("第%d个学生姓名和成绩:", i + 1);
scanf("%s%d",s->name,&(s->score));//需修改
tu->next = s;
tu = s;
}
tu->next = NULL;
}
void DispList(StudList *L)
{
StudList *p = L->next;
printf(" 名次 姓名 成绩\n");
int i = 1;
while (p != NULL)
{
printf(" %d\t\t", i++);
printf("%s\t\t", p->name);//有错
printf("%d\n", p->score);
p = p->next;
}
}
int main()
{
StudList *n=NULL;
printf("建立学生表\n");
CreateStudent(&n);
DispList(n);//需修改
system("pause");
return 0;
}
运行结果如下: