链表的的创建和输出

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)


struct Student{
   long num;
   float score;
   struct Student* next;
};
int n;
struct Student* create(void)
{
   n=0;
   struct Student *head,*p1,*p2;
   head=NULL;
   p1=p2=(struct Student*)malloc(LEN);
   scanf("%ld%f",&p1->num,&p1->score);
   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;
    p=head;
    while(p!=NULL)
    {
        printf("%ld %.1f\n",p->num,p->score);
        p=p->next;
    }
}
int main()
{
    struct Student *head;
    head=create();
    print(head);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39350434/article/details/81000230