C Language Learning 016: single-chain table, C Language Learning 016 single-chain

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.