在一个寻宝游戏中,你从第一个线索开始寻找,当你找到线索后,而不是得到宝藏,而是拥有下一个线索的地址,一直搜寻线索,直到找到宝藏。
链表是类似的结构,是一系列连接的nodes,每个node包含下一个node的地址,每个节点也包含一个数据。
链表展现形式:
在链表中的第一个节点叫做HEAD,链表的最后一个节点也可以确认,因为它指向下一个节点为NULL。
一个节点包含:
- 数据
- 另一个节点的地址
节点的结构如下:
struct node
{
int data;
struct node *next;
};
创建一个链表
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->