2 dxskx dxskx 于 2016.09.07 17:40 提问

c语言链表问题,求助啊 20C

#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;

}

大神们,我每次运行的时候都提示我注释的地方有异常,谁能帮我调一下,有赏啊,兄弟们

6个回答

waterhexuan
waterhexuan   2016.09.07 19:26

你这代码 问题很多
DispList(&n); &n 是 StudList** 类型 而你的函数 原型 需要 StudList* 类型
scanf("%s-%d", s->name,&s->score); 好像 并不能正确的输入
sl = (StudList)malloc(sizeof(StudList)); 这类型 都不一致 不知道你怎么能编译不报错呢

u011847710
u011847710   2016.09.08 09:43

仅供参考

 #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;
}

kaixinyixiao33
kaixinyixiao33   2016.09.07 19:28

*pstu = (StudList *)malloc(sizeof(StudList));
malloc开辟空间后返回指针变量,该指针指向所申请的空间地址。StudList s.你定义的s是个变量不是StudList类型的指针。

caozhy
caozhy   Ds   Rxr 2016.09.07 22:37
kingLeeST
kingLeeST   2016.09.07 23:30

推荐你去看一下浙大翁恺老师的C语言课程,其中有链表的讲述

qq_27587417
qq_27587417   2016.09.08 11:39

修改了部分代码,已经可以正常运行了

#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;
}

运行结果如下:
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!