链表

来源:互联网 发布:成都瑞星数据恢复 编辑:程序博客网 时间:2024/06/05 18:35

链表:动态的进行存储分配的一种结构

节点包括:数据域 和指针域

p1指的是新建的节点,包括指针域和数据域。

#include<stdio.h>

#include<malloc.h>

struct student{ long num;      float score; struct student *next;};

int main()
{
        struct student *head,*p1,*p2;
        int i,len;
        len=sizeof(struct student);
        p1=p2=(struct student*)malloc(len);
        scanf("%ld,%f",&p1->num,&p1->score);
        head = NULL;
        while(p1->num!=0)
{
        if(head==NULL)
                 head=p1;//头指针执行p1节点
        else
               { p2->next=p1;//p1新开辟的节点链接到p2的后面
        p2=p1;}
        p1=(struct student*)malloc(len);
        scanf("%ld,%f",&p1->num,&p1->score);
        p2->next = NULL;

}

//输出链表数据

        struct student *p;
        p=head;
        do{
        printf("%ld%5.1f\n",p->num,p->score);
        p=p->next;
}while(p!=NULL);
return 0;

}




原创粉丝点击