1 #include <stdio.h>
2
3 //定義一個鏈表,鏈表是一種遞歸結構,在定義的時候必須要給結構起一個名字
4 typedef struct folder{
5 int level;
6 char* filename;
7 struct folder* child;//通過指針鏈接下一個結構
8 }folder;
9
10 int main(){
11 folder first={1,"first",NULL};
12 folder second={2,"second",NULL};
13 folder thread={3,"thread",NULL};
14 first.child=&second;
15 second.child=&thread;
16 folder* i=&first;
17 for(;i!=NULL;i=i->child){
18 printf("%s level is %i \n",i->filename,i->level);
19 }
20 return 0;
21 }
在鏈表中插入值,只需要修改指針的值就行
second.child=&thread;
folder fourth={4,"fourth",NULL};
鏈表相對於數組而言,插入數據非常快,但是如果有一個很長的鏈表,要想訪問最後一個元素,你需要從第一個開始一層一層的讀下去,而數組可以通過索引直接訪問元素,所以使用數組還是鏈表需要根據環境來決定