C语言.动态链表.随笔

来源:互联网 发布:powell算法 编辑:程序博客网 时间:2024/05/20 06:07
#include<stdio.h>#include <stdlib.h>#include <stddef.h>#define LEN sizeof(struct student)struct student{    int num;    float score;    struct student *next;};int main() {    struct student *p1,*p2,*head,*p;    int n=0;    head=NULL;//赋值 防止出错    p1=p2=(struct student*)malloc(LEN);//申请一块区域,将首                                       //地址传给p1=p2。    scanf("%d %f",&p1->num,&p1->score);    while((p1->score)>=0){        //分数小于零结束        n++;        if(n==1) head=p1;  //第一块区域,将首地址赋值给head保存        else p2->next=p1;           p2=p1;        p1=(struct student*)malloc(LEN);        scanf("%d %f",&p1->num,&p1->score);    }    p2->next=NULL;  //为最后一块区域的地址赋给空值,方便输出    p=head;        //输出    while(p!=NULL){    printf("%d %f\n",p->num,p->score);    p=p->next;    }}

如果发现问题 谢谢 及时与我联系 QQ 991224317

原创粉丝点击