昨日回顧
struct student
{
int ID;
char name[32]; //next存儲的是下一個結(jié)點的地址
struct student *next;
};
//頭結(jié)點:
//指針head指向malloc得到的空間的地址,用于存放數(shù)據(jù)
//頭結(jié)點的數(shù)據(jù)域為空,指針域存儲的是下一個結(jié)點的地址!!
struct student *head = (struct student*)malloc(sizeof(struct student));
head->next = NULL;
//要插入的結(jié)點
struct student *temp = (struct student*)malloc(sizeof(struct student));
temp->ID = 12;
strcpy(temp->name,"zhang");
temp->next = NULL;
//因next存儲的是第一個結(jié)點的地址,故訪問到next,就可以訪問到
//下一個結(jié)點
//將要插入的結(jié)點鏈接頭結(jié)點之后
//head->next:第一個結(jié)點的地址
//temp->next: temp后面的第一個結(jié)點的地址
temp->next = head->next;
//重新定向頭結(jié)點的下一個結(jié)點的地址
head->next = temp;
//temp所指向的空間已經(jīng)添加到鏈表上,為防止其成為野指針,
//故將其置空即:
temp = NULL;
排版5分鐘