[C Resolution 10] linked list advanced

Source: Internet
Author: User

C parsing-10 linked lists (advanced tutorial)
The most intuitive illustration is the most confusing point of the linked list. Next we will introduce the core operations of the linked list: insert, delete, query, master these operations, the linked list can shine in your program.1. Insert a linked list: Insert a new node to the specified position of the linked list.For example:
The new node to be inserted (p) is inserted between two nodes in the Linked List (p1 on the left and p2 on the right). To complete this operation, two steps are required: 1. point the pointer of p1 to the node waiting for insertion; 2 point the pointer of the node waiting for insertion to p2. ().The numbers 1 and 2 indicate the two operation steps respectively. Key code for inserting a node:P1-> next = p;P-> next = p2;2. delete a linked list: delete a node from the linked list.For example:Deleting a node is the inverse operation to insert a node in the previous step. Delete node P. You only need to point the pointer of P1 to P2, and P does not belong to the linked list. ().The number 1 indicates the step required for deletion. Some may think that the pointer of P still points to P2, which cannot change the fact that P has been deleted by the linked list. Core code of the chain table deletion operation:P1-> next = P-> next; // P-next points to P2Or: P1-> next = P2;3. Linked List query: You can search for nodes that meet specific requirements.To query a linked list, you need to traverse each node. During the traversal process, you can also output the linked list, at the end of the traversal, you can find the end of the linked list (here we also explain the output of the linked list and the end of the linked list that readers may have doubts in the preliminary study of the [C Resolution] Chain List ).For example:Instead of storing data in the Head of the table, the nodes after the Head are actually the nodes with data. Whether it is the comparison during query or the output of time, the data in the linked list node is operated. We define a pointer to access all nodes through the pointer offset (pointing to each node in turn.Analysis in sequence:Number 1: point the pointer to the first linked list node. Code: P = Head-> next;Number 2: The Pointer Points to the next linked list node. Code: P = P-> next;Number 3: The Pointer Points to the next node. Code: P = P-> next;......The final result is P-next = NULL. At this time, the point pointed by P is the end node, which can be one of the conditions for controlling the while loop.Continue to expand the previous example: this example is a good example for beginners to further learn C. If you are interested, try it. To understand this example, the C language is now a preliminary introduction. At the same time, this example can be described as a dirty one for linked list operations. With the help of the illustration above, read carefully and understand the relationship. I believe that linked list operations can best train logical thinking and test developers' levels of detail. For beginners, I decided not to add more interference, and the next program will not be modularized.

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.