C Language Learning 016: single-chain table, C Language Learning 016 single-chain
1 # include <stdio. h> 2 3 // define a linked list. The linked list is a recursive structure. You must name the structure 4 typedef struct folder {5 int level; 6 char * filename; 7 struct folder * child; // the pointer is used to link the next structure 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}
Insert a value to the linked list. You only need to modify the pointer value.
Second. child = & thread; folder fourth = {4, "fourth", NULL };
The linked list inserts data very quickly relative to the array, but if there is a long linked list, to access the last element, you need to read it from the first layer, arrays can directly access elements through indexes. Therefore, the use of arrays or linked lists depends on the environment.