也就是说为什么创建一个节点的时候是LNode* p,而为什么不可以是LNode p 这样子p的指针域不是也存了下一个节点的位置吗?
求解答
在链表中,每个节点的指针指向的是与该链表结构一样的节点。链表定义的结构体,就是描述链表结构中节点的组成。定义的指针呢,是种嵌套,指向结构体本身。而结构体是一种数据类型,而不是具体的一个变量。所以这里可以理解为定义的指针指向的是,该链表的某个节点。
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int main () {
// 创建 第一个节点
struct node *head = (struct node*)malloc (sizeof (struct node));
head->data = 1;
head->next = NULL;
// 创建 第二个节点
struct node *second = (struct node*)malloc (sizeof (struct node));
second->data = 2;
second->next = NULL;
// 将第二个节点链接到第一个节点的后面
head->next = second;
return 0;
}
区别就是一个是动态的生成一个结点,一个是静态的结点。