shi981123 发表于 2020-8-17 20:12:36

按自己理解写的链表代码

#include<stdio.h>
#include<string.h>
struct student
{
        int num;
        float score;
        struct student* next;
};
#define LEN sizeof(struct student)
struct student* creat();
void scan(struct student* head);
void print(struct student* point);
int main()
{
        struct student* head = (struct student*)malloc(LEN);
        struct student* point = head;
        scan(head);
        print(head);
}

struct student* creat()
{
        struct student* list = (struct student*)malloc(LEN);
        list->num = 0;
        list->score = 0;
        list->next = NULL;
        return list;
}
void scan(struct student* head)
{
        printf("please enter the num:");
        scanf_s("%d", &(head->num));
        printf("please enter the score:");
        scanf_s("%f", &head->score);
        while (head->num)
        {
                head->next = creat();
                head = head->next;

                printf("please enter the num:");
                scanf_s("%d", &head->num);
                printf("please enter the score:");
                scanf_s("%f", &head->score);
        }
}
void print(struct student* point)
{
        while (point->num)
        {
                printf("num:%d\tscore:%.2f\n", point->num, point->score);
                point = point->next;
        }
}









页: [1]
查看完整版本: 按自己理解写的链表代码