链表输入输出函数

/*建立动态链表*/
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat(void);
void print(struct student *head);
struct student
{
    long int num;
    float score;
    struct student *next;
};
int n;
int main()
{
    struct student *p;
    p = creat();
    print(p);
    return 0;
};
struct student *creat(void)/*输入函数*/
{
    struct student *head, *p1, *p2;
    int n = 0;
    p1 = p2 = (struct student *)malloc(LEN);
    scanf("%ld%f", &p1->num, &p1->score);
    head = NULL;
    while (p1->num != 0)
    {
        n++;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (struct student *)malloc(LEN);
        scanf("%ld%f", &p1->num, &p1->score);
    }
    p2->next = NULL;
    return head;
}
void print(struct student *head)/*输出函数*/
{
    struct student *p;
    printf("this is the data of student after dispose:\n");
    p = head;
    if (head != NULL)
    {
        do
        {
            printf("\nnum:%ld\nscore:%5.1f\n", p->num, p->score);
            p = p ->next;
        } while (p != NULL);
    }
}

猜你喜欢

转载自blog.csdn.net/progess_every_day/article/details/104880330