链表由“结点”组成,结点由“数据部分”和“指针部分”组成
完整的一个链表
特点:各个成员“结点”,不是连续存储的,最后一个成员的指针是NULL
链表的表示(使用指向第一个结点的指针,来表示整个链表)
空链表
链表的代码实现
节点数据类型
struct node {
int value
struct node *next
}
#include<stdio.h>
struct node {
int value;
struct node* next;//指向下一个结点
};
int main(void)
{
struct node a;//一个结点
struct node b;
struct node* list = NULL;
list& a;
a.next = &b;
b.next = NULL;
return 0;
}但是正式用的不会这样用
链表的类型
头节点
不带头节点的链表:(不好用)
链表list包含的数据,1,2,3,4
不浪费每一个成员(结点)都包含实实在在的数据,也是最原始的
带头节点的链表:
链表list中包含的数据,1,2,3
头节点,没有包含任何数据。除了头结点以外,每个节点都有一个“前驱”结点(就是每一个有数据的结点前面都有一个节点,这给我们创造了极大的便利)
链表list中包含的数据(空链表)
方向
链表的基础操作(带头结点)
链表的创建
创建空链表(带头结点的)不带头结点直接return NULL好了
struct node* list_create() {
//malloc申请内存失败,则返回NULL,
struct node* list = (struct node*)malloc(sizeof(struct node));
if (list=NULL) {
return NULL‘
}//创建头结点
return list;
}malloc是从堆空间申请内存,堆空间是永久存在的,这不同于栈空间