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

2016-12-23  本文已影响0人  穹蓝奥义

昨日回顾

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分钟

上一篇 下一篇

猜你喜欢

热点阅读