建立一个链表存储学生信息。


#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(Student)
typedef struct 
{
    char no[8];//8位学号
    char name[20];//姓名
    int price;//成绩
    Student * next;
}Student;
int main()
{
    Student* Build_List();
    void Print(Student * head);

    Student * head;
    //根据指定学生个数,逐个输入学生信息
    head = Build_List();
    //逐个显示学生表中所有学生的相关信息
    Print(head);
    //根据姓名进行查找,返回此学生的学号和成绩
    return 0;
}
Student* Build_List()
{
    int n;
    Student* head, * p1, * p2;
    head = NULL;
    p1 = p2 = (Student*)malloc(LEN);
    scanf_s("%s", p1->no, 8);
    scanf_s("%s", p1->name, 20);
    scanf_s("%d", &p1->price);
    while (p1->no != 0)//p1->no==0标志输入结束,链表建立完成。
    {
        n = n + 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (Student*)malloc(LEN);
        scanf_s("%s", p1->no, 8);
        scanf_s("%s", p1->name, 20);
        scanf_s("%d", &p1->price);
    }
    p2->next = NULL;
    return head;
}
void Print(Student* head)
{
    Student* p;
    p = head;
    printf("-------------------------Student List-------------------------\n");
    printf("NO.       NAME          SCORE\n");
    while (p != NULL)
    {
        printf("%8s%10s%5d\n", p->no, p->name, p->price);
        p = p->next;
    }
}

img

代码我放这了,这是怎么回事啊

typedef struct
{
char no[8];//8位学号
char name[20];//姓名
int price;//成绩
Student * next;//简单点说,至此Student 还没有定义 不能使用,Student 出现在后边,用不上。
}Student;

typedef struct Student //起个名字,不要匿名,以后要用
{
char no[8];//8位学号
char name[20];//姓名
int price;//成绩
struct Student* next;
}Student;

typedef struct stu //起个名字,不要匿名,以后要用
{
char no[8];//8位学号
char name[20];//姓名
int price;//成绩
struct stu* next;//编译cpp 代码时stu* next;也可以
}Student;


#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(Student)
typedef struct
{
    char no[8];//8位学号
    char name[20];//姓名
    int price;//成绩
    struct Student* next;
}Student;
int main()
{
    Student* Build_List();
    void Print(Student * head);

    Student* head;
    //根据指定学生个数,逐个输入学生信息
    head = Build_List();
    //逐个显示学生表中所有学生的相关信息
    Print(head);
    //根据姓名进行查找,返回此学生的学号和成绩
    return 0;
}
Student* Build_List()
{
    int n = 0;
    Student* head, * p1, * p2;
    head = NULL;
    p1 = p2 = (Student*)malloc(LEN);
    scanf_s("%s", p1->no, 8);
    scanf_s("%s", p1->name, 20);
    scanf_s("%d", &p1->price);
    while (p1->no != 0)//p1->no==0标志输入结束,链表建立完成。
    {
        n = n + 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (Student*)malloc(LEN);
        scanf_s("%s", p1->no, 8);
        scanf_s("%s", p1->name, 20);
        scanf_s("%d", &p1->price);
    }
    p2->next = NULL;
    return head;
}
void Print(Student* head)
{
    Student* p;
    p = head;
    printf("-------------------------Student List-------------------------\n");
    printf("NO.       NAME          SCORE\n");
    while (p != NULL)
    {
        printf("%8s%10s%5d\n", p->no, p->name, p->price);
        p = p->next;
    }
}