使用头插法创建链表并输出

//使用尾插法创建链表并输出 
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
struct Student{
    long long num;
    float score;
    struct Student *next;
};
//建立创建链表的函数
struct Student *creat(){
    struct Student *head,*p1,*p2;
    head=(struct Student*)malloc(LEN);
    long long num;float score;
    scanf("%ld%f",&num,&score);
    head->next=NULL;
    while(num!=0){
        p1=(struct Student*)malloc(LEN);
        p1->num=num;
        p1->score=score;
        p1->next=head->next;
        head->next=p1;
         scanf("%ld%f",&num,&score);
    }
    return head;
}
void print(struct Student *head){
    struct Student *p;
    printf("输出信息:\n");
    p=head;
    p=p->next;
    while(p!=NULL){
        printf("%ld   %f\n",p->num,p->score);
        p=p->next;
    }
}
int main()
{
    struct Student *pt;
    pt=creat();
    print(pt);
    return 0;
}

  收录于文章《885程序设计考点狂背总目录中

posted @ 2020-08-18 19:58  薄眠抛却陈年事。  阅读(282)  评论(0编辑  收藏  举报