学习笔记 链表的创建和赋值

#include

#include "stdlib.h"

struct node

{

    int date;

    struct node *next;

};

int main()

{

    int n;

    scanf("%d",&n);

    struct node *p1 = NULL,*p2 = NULL;

    struct node *head=NULL; //首先创建3个指针地址 并且全部赋值为空

    for (int i=0;i

        p2=(struct node *)malloc(sizeof(struct node)); //为其他保存值的指针开辟空间

        p2->date=i+1;               //在头指针为空的情况下 把值指针的首地址给首指针

        if (head==NULL) {

            head=p2;

        }

        else{p1->next=p2;}

         p1=p2;                      //保存首地址后 同时把指针的首地址给与保存地址指针 这样实现了首指针和保存地址的指针一致 //需要保存地址的指针和它内部的指针一致 这样调用next函数才能调用到地址 不然没对象

    }

    //从而实现的首部指针 保存的的指针可以调用保存地址的指针

    while (head) {

        printf("%d ",head->date);

        head=head->next;           //利用指针的循环

    }

    return 0;

}


你可能感兴趣的:(学习笔记 链表的创建和赋值)