#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
struct film{
char name[20];
int id;
struct film *next;
};
//--------创建3个节点的链表---------
struct film f3={
"wl",
3,
NULL
};
struct film f2={
"wh",
2,
&f3
};
struct film f1={
"wz",
1,
&f2
};
//-------遍历---------------
struct film *p=&f1;
while(p!=NULL){
printf("%d\n",p->id);
p=p->next;
}
return 0;
}
从此代码可以看出,链表的作用就是把分散的不连续的多个内存关联起来,如和malloc联用,可以分配任意多个非连续内存。