培训第十二天----部分链表细节

昨日回顾

struct student
{
    int ID;
    char name[32];      //next存储的是下一个结点的地址
    struct student *next;   
};

//头结点:
//指针head指向malloc得到的空间的地址,用于存放数据
//头结点的数据域为空,指针域存储的是下一个结点的地址!!

struct student *head = (struct student*)malloc(sizeof(struct student));
head->next = NULL;

//要插入的结点

struct student *temp = (struct student*)malloc(sizeof(struct student));
temp->ID = 12;
strcpy(temp->name,"zhang");
temp->next = NULL;

//因next存储的是第一个结点的地址,故访问到next,就可以访问到
//下一个结点
//将要插入的结点链接头结点之后
//head->next:第一个结点的地址
//temp->next: temp后面的第一个结点的地址

temp->next = head->next;

//重新定向头结点的下一个结点的地址

head->next = temp;

//temp所指向的空间已经添加到链表上,为防止其成为野指针,
//故将其置空即:

temp = NULL;

排版5分钟

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容