结构体链表 (创建)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
    int no;
    char name[21];
    double s[3];
    double ave;
    double sum;
}stu;
typedef struct node
{
    stu date;
    struct node *next;
}lnode;
void input(lnode *p)
{
    int i;
    int no;
    char nm[21];
    double fs[4];
    scanf("%d",&no);
    scanf("%s",nm);
    p->date.no=no;
    strcpy(p->date.name,nm);
    p->date.sum=0;
    for(i=0;i<3;i++)
    {
        scanf("%lf",&fs[i]);
        p->date.s[i]=fs[i];
        p->date.sum+=fs[i];
    }
    p->date.ave=p->date.sum/3;
}
void creat(lnode *h,int n)
{
    lnode *p,*r;
    int i;
    r=h;
    for(i=0;i<n;i++)
    {
        p=(lnode *)malloc(sizeof(lnode));
        input(p);
        r->next=p;
        r=p;
    }
    r->next=NULL;
}
void output(lnode *h)
{
    int i;
    lnode *p;
    p=h->next;
    while(p!=NULL)
    {
        printf("%d  %s  ",p->date.no,p->date.name);
        for(i=0;i<3;i++)
        {
            printf("%.2lf  ",p->date.s[i]);
        }
        printf("%.2lf  %.2lf\n",p->date.ave,p->date.sum);
        p=p->next;
    }
}
int main()
{
    int n;
    lnode *h;
    while(scanf("%d",&n)!=-1)
    {
        h=(lnode *)malloc(sizeof(lnode));
        h->next=NULL;
        creat(h,n);
        output(h);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xqx1343002589/article/details/80428082