- 链表实现了,内存零碎数据的有效组织。比如,当我们用 malloc 来进行内存申请的时候,当内存足够,但是由于碎片太多,没有连续内存时,只能以申请失败而告终,而用链表这种数据结构来组织数据,就可以解决上类问题。
![](https://camo.githubusercontent.com/03b308bd822e6f5f1101821f2240a3bbe71b4e1605666317c367b633c3b62f3c/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f696d675f636f6e766572742f65626132303739323737396439643961393965376434666630643233656631332e706e67)
![](https://camo.githubusercontent.com/30f7b2088023435bd2a5c29e63a2b66c9d14fab7245674f6dbad254789f9e836/68747470733a2f2f696d672d626c6f672e6373646e696d672e636e2f696d675f636f6e766572742f34346135373232633931373037316539333764613433356665353639356132362e706e67)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 1.定义链表节点
typedef struct node{
int data;
struct node *next;
}Node;
int main()
{
// 2.创建链表节点
Node a;
Node b;
Node c;
// 3.初始化节点数据
a.data = 1;
b.data = 3;
c.data = 5;
// 4.链接节点
a.next = &b;
b.next = &c;
c.next = NULL;
// 5.创建链表头
Node *head = &a;
// 6.使用链表
while(head != NULL){
int currentData = head->data;
printf("currentData = %i\n", currentData);
head = head->next;
}
return 0;
}