C语言建立动态链表

//////////////////////////////
///      建立动态链表      ///
//////////////////////////////
#include 
#include 
#define NULL 0
#define LEN sizeof(struct student) //"sizeof"求字节数运算符
struct student
{
    long num;
    float score;
    struct student *next;
};
/* 使用函数建立动态链表,带回一个头指针 */
struct student *Creat(void)
{
    struct student *head=NULL;
    struct student *p1,*p2;
    p1 = p2 = (struct student *)malloc(LEN); //开辟一个新单元
    scanf("%ld%f", &(p1->num), &(p1->score));
    int first=1;
    while(p1->num != 0) //输入作为"0 0"结束标志
    {
        if(first) {head=p1; first=0;}
        else p2->next = p1;
        p2 = p1;
        p1 = (struct student *)malloc(LEN);
        scanf("%ld%f", &p1->num, &p1->score);
    } /*p2指向链表中最后一个结点,p1指向新开辟的结点,
    用"p2->next=p1"连接两个结点,通过这种方式延长链表*/
    p2->next = NULL;
    return head;
}
int main()
{
    struct student *pt;
    pt = Creat();
    return 0;
}

你可能感兴趣的:(C语言建立动态链表)