#include <stdio.h> //定义一个链表,链表是一种递归结构,在定义的时候必须要给结构起一个名字
typedef struct folder{
int level;
char* filename;
struct folder* child;//通过指针链接下一个结构
}folder; int main(){
folder first={,"first",NULL};
folder second={,"second",NULL};
folder thread={,"thread",NULL};
first.child=&second;
second.child=&thread;
folder* i=&first;
for(;i!=NULL;i=i->child){
printf("%s level is %i \n",i->filename,i->level);
}
return ;
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoIAAAA2CAIAAADcVm8IAAAD+ElEQVR4nO3dUVbbRhgGUG8pdCFJ47SEwELadB2hTUrJIgKkj15CQrqTpE2x+9CHHnxcjUaaGdkaG937JI9Hn378wAfmyMyevvr87sPdo8c/zgCAkalhAKhGDQNANWoYAKpRwwBQTaOGF/fCnbH13Vls2J/8bc0z8osJwD7arOF1wbQ2zVbqpyBh1/VfpYbH/5kGgH309Pzzu4/NGt6Rsvz9nGrg5dQwALNZWg23NsfmSuyp2GJWCYWbO/JbT+m+aPowsZytfFEATNG6hns7MlY/s3gldzRlusS6zT0unipxnqwQACYq/U3pWA33tm9HQorW2tsUbguPw/3FU8Veh/SEslMAeICG1/C29ifmpHR8ys8ExVOlDFAcAsC0bL2GOypw/XBIY/Xmp+/vWNzKPOkhAEzUvO1vw409i8As0nnh/thTKbO1XjclP3GeWP4W50kPAWCK5uc+RQsAKlHDAFDN/NWXqw93jx7/UHsQAJie+bkaBoBK7t+UVsMAMLr7G5bUMACMbq6GAaCWZ+dfrvajhstuvd3pJFn5w+dxMzHA5Dz7+c+rj/tyw1JWA+26rkau4fDzQIqjADgY3/3y1/Xt8kgNj57fcTk1DDAV37/+en27PHrycpb//3Sz1hf/F9s/pIY7rtt6SvdF04fJfX0SA7NOAeAgHb/+en27OnryMlZXtY57JdZt8XVzuzBxntwcAB6y4zd/39yuvvn2p1nC/0VIqZa6Ndw9Z3gc7o/l585TkFB2CgAH7PjN3bqG/9Nab+GJ+1nDZfOk5OfOU5CjgwEm5/l9DW+rPvehhlufSt9fMEzWPL2n62OAqXj+613jTemwA7aynlKT4VOtFoHueWKxKfPvaJ70EAAess0aBgBGpYYBoJqTCzUMAJWcXCxvPqlhAKhBDQNANWoYAKo5uVjefPpHDQNABS/6ani021jLbrHd6SRZ+cPncdMwwOS8+G35fqOGWwtgtGLIutCupxq5hsPP/SiOAuBgbNZw7Lu/Gh6ZGgaYinUNLwLrPeuH4W9s4fos7UMiY+tDarh7+PQ5Y/ndk6R/vYmBWacAcJDSfxsOG7ex2LGhNbD7uFdi3eYex/K3NU9KiA4GmIrcN6XD40b7LgKNnMZ6WV2Fm2PX7R0+1ny5dRjuz00YfiIAB+Z0cA13bM4NGV7DA4fvyM+dpzin7BQADlJrDXc0XFYNDznulTjk+mH6/oJhsubpPV0NA0zFaXDDUqzDNh/OIt3WOKXx1OZimBmuxywC3deNxfbOGUsbPk9vTsp+AA5eo4YBgPGcXqphAKjk7HKlhgGgjrPL1fs/1DAA1HB2ufpdDQNAFWdv1TAAVKKGAaAaNQwAtfwL7MC8NUvd8rEAAAAASUVORK5CYII=" alt="" />
在链表中插入值,只需要修改指针的值就行
second.child=&thread;
folder fourth={,"fourth",NULL};
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAAzCAIAAACIbi6GAAAEGklEQVR4nO3dMY8bRRgG4P0FASFR0NAgQVAkogsVDRXNoYhDoqGio6OhQAJ+xl2EhERDg8QFRQKfaGhMQUMSuJPSUNFR0VAg5agoDNbmZnd2Zmzv2N7nqdbr2W9eX5H3nHidZnZ+eXD0SQMAjGl2fnlw9HHtFAAwMbPzxwoYAMamgAGgAgUMABW0C3j+v3BZ3/nNmbdsz/x15Rn5hwnA1ll+CGtZLZ0ds5biKZiw6eKvUsDj/zYDwNaZXfx3G9K2Vd0qV23P/M7tFDAAzdnF5a1oAXd2RvtM31N9J7PqJ1wcmd95SXzT9DB9c9byogCYnLOLf269/elgO/YVT9NfxpGOTJdYtLnHxakS82QNAWCKFgXcDLVCXwEP9m5kQorOwmsLl4XH4friVH0/h/QJZZcAsG/O0v4NOLd41lJU4VUp7Z7y20BxqpQAxUMAmJDZr48P3nriU9CdEgs1Un7Lh6t01eD89PWRk2vJkz4EgCn69uHfr9z+qLO9FuaBpqftwvV9T6UE69w3ZX5inr75a8yTPgSAybn38183Dj+snQIAJub0pz+vv/FB7RQAMDFf/fjHC6+/XzsFAEzMlz/8/vxr79VOAQAT88X3vz336ru1UwDAxHz+3aNnb75TOwUATMxn986fuXFUOwUATMydb355+uXbtVNk3CK86Ztoy+avnsfNwQDTcnL3wVPX36ydomm27NscRy7g8Js9ikcBsBuOT+9fe+mwdoqm2fECXuN2ChhgEo6/vn/txcMm//+7zTo/f1Lf+lUKOLJv5yXxTdPD5P58EgdmXQLA7lkUcF9R1ToelFi0xfvmtmBintw5AOytK++Arzw7D7TPh9PqFnA8Z3gcru+bn5unYELZJQDsqmUBL3QWW3jVdhZwWZ6U+bl5CuZoX4BpOT7d57+CXj5MX18QJivP4OWaGGASFgXcjPIhrPC4vT58qtM8EM/TNzYl/4bypA8BYG8tCxgAGI8CBoAKThQwAIzv5PSBAgaAsSlgAKhAAQNABQoYACpoF/D4N6EW7Ljpm2XL5q8xj/uAASZhWcDLP/c32m2dJwu223RL1SpgX8QBMBVhAW9I3/z9KOA1bqqAASZhsIA7W6H9MDwO30nPA+GorOIJF2fl7FsfmR9PkphncE7W1gDssEUBD7Zj4nETFEnfsivrc7tnxZyDwXJbMDFPyuW5WwOwk+LvgIsLOCzUeAHH18Svam/a3n0wc7i+b35untwJYZ6s3QHYPSd3N1LA4UYp59OLp2DTrI1WL+CyOWWXALB7xi/gyPq1FHDnU7mbrljAZS+qbD0AO2lRwJ0ttdD3VPvklYN4B3e2Y/thPPA8EM/ZN3bwdUVeyIp5EqelXwXA7lm+AwYAxnNHAQPA+BQwAFSggAGgAgUMAOP7F8+BJ4SCeR1QAAAAAElFTkSuQmCC" alt="" />
链表相对于数组而言,插入数据非常快,但是如果有一个很长的链表,要想访问最后一个元素,你需要从第一个开始一层一层的读下去,而数组可以通过索引直接访问元素,所以使用数组还是链表需要根据环境来决定