C语言学习016:单链表

 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 
",i->filename,i->level);
19     }
20     return 0;
21 }

  在链表中插入值,只需要修改指针的值就行

    second.child=&thread;
    folder fourth={4,"fourth",NULL};

  链表相对于数组而言,插入数据非常快,但是如果有一个很长的链表,要想访问最后一个元素,你需要从第一个开始一层一层的读下去,而数组可以通过索引直接访问元素,所以使用数组还是链表需要根据环境来决定

原文地址:https://www.cnblogs.com/liunlls/p/C_list.html