链表链表...神奇的链表

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define len sizeof(struct student)
using namespace std;
struct student
{
    long num;
    float score;
    struct student *next;                 //声明一个指针变量  其中的    这个用structure student 定义   用next  储存  地址...........
};
using namespace std;
int n;
struct student *creat()                  //开辟一个新节点,
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(len);   //开辟一个长度为  len的 空间    用强制转换类型  获得它的首地址.....
    scanf("%ld%f",&p1->num,&p1->score);    //
    head=NULL;                             //
    while(p1->num!=0)                      //  约定学好不为0....
    {
        n++;
        if(n==1)                          //这是第一个节点  所以
            head=p1;                      // 让head   记录下来头结点的地址....
        else
            p2->next=p1;                  //                        //让上一次开辟的  next部分储存起来  刚才开辟的   p1的地址.... 
        p2=p1;                           //  p2 和head  都和p1相同....                       //将刚才开辟的 空间的首地址  赋予p2       (方便下一次又开辟的空间的首地址  向 刚才开辟的位置存放....)
        p1=(struct student *)malloc(len);//在此开辟一个新空间  p1  从原来的地址变成了现在的地址........
        scanf("%ld    %f",&p1->num,&p1->score);  //然后就要返回检查学号了.    
    }
    p2->next=NULL;
    return head;
}
void print(struct student *head)
{
    struct student *p;
    p=head;
    if(head!=NULL)
    {
        do
        {
            printf("%ld%5.1f
",p->num,p->score);
            p=p->next;
        }while(p!=NULL);
    }
}
void main()
{
    struct student *head;
    head=creat();
    print(head);
}
原文地址:https://www.cnblogs.com/A-FM/p/5049532.html