Analysis of an insert operation in a single-chain table

Source: Internet
Author: User

First, let's look at the structure of the linked list: p-> node .... (one-way), the node consists of two parts: Value and pointer link pointing to the next node. P is the root node and only the pointer field. Its type is the pointer pointing to the node, if you want to modify the parameter, you need to pass its address to the function as a real parameter when calling the function, and the type of the parameter of the function should be a pointer to the pointer.
When performing some linked list operations, such as node insertion, inserting a node to the starting position of the linked list must be processed as a special case, this is because we want to modify the pointer to the pointer. For other locations, we need to modify the link field of the node. It seems that we have to write more code for the operation on the linked list, however, these two seemingly different operations are actually the same.
The key to elimination is: we must realize that each node in the linked list has a pointer to it. For the first node, this pointer is the root pointer! For other nodes, this pointer is the link field of the previous node. The point is that each node has a pointer pointing to it. It does not matter whether the node is inside a node.
After clearing the train of thought, the solution is provided: we modify the head variable in the same way as modifying the node link field. Finally, add the register declaration to the pointer variable of the function, it is used to improve the efficiency of the result code.
Attach the insert operation of the linked list in "C and pointer" and briefly parse it. This is mainly a pointer issue.

# Include <stdio. h>
# Include <stdlib. h>
# Define false 0
# Define true 1
Typedef struct node // defines the linked list struct
{
Struct node * link;
Int value;
} Node;
Int insert (register node ** linkp, int new_value)
{
Register node * current;
Register node * new_node;
/* Find the insert location. The method is used to traverse the table until a node with a value greater than or equal to the new value is reached */
While (current = * linkp )! = NULL & Current-> value <new_value)
Linkp = & Current-> link;
/* Allocate memory for the node */
New_node = (node *) malloc (sizeof (node ));
If (new_node = NULL)
Return false;
New_node-> value = new_value;
/* Insert the node and return true */
New_node-> link = current;
* Linkp = new_node;
Return true;
}
/* The following is the test program */
Main ()
{
Node * head = NULL; // defines a pointer to a node as the root node.
Node one, second; // initialize two nodes
Head = & one; // point the header pointer to the first node
If (Head! = NULL)
Head-> value = 5;
Head-> link = & second;
(& Second)-> value = 10;
// Display the linked list
Printf ("first node: % 5d, address: % 10d/N", head-> value, & one );
Printf ("Section 2: % 5d; Address: % 10d/N", (Head-> link)-> value, head-> link );
Printf ("after the insert operation is executed:/N ");
Insert (& head, 3); // call the insert Function
Printf ("first node: % 5d, address: % 10d/N", head-> value, & one );
Printf ("Section 2: % 5d; Address: % 10d/N", (Head-> link)-> value, head-> link );
Printf ("third node: % 5d, address: % 10d/N", head-> link-> value, head-> link );
Printf ("good ~ /N ");
}
The debugging procedure is relatively simple. The following describes the execution process of the insert () function. The function parameter is a pointer to the node and the value to be inserted. The first parameter is used to transmit the value through the address to return the modification of the linked list, the real parameter uses the & head, that is, the address of the root node. When executing the insert operation, copy the pointer to the root node first (not the pointer to the root node !) And the data to be inserted is transmitted to insert (). In the traversal table, first assign the head value, that is, the root node value, that is, the address of the first node to current, at this time, Curren and head point to the first node at the same time, and then start to judge. The linkp moves along with the current pointer (because the current = * linkp is executed in each loop ). After the search is complete, the memory is dynamically allocated to generate a new node and direct it to the current node. * linkp points to it.
Here is a small problem that makes me think about two days. First, let's look at the insert function. When it is executed, it assigns & head to linkp and uses the address to pass the value, at the end of the function, * linkp is changed (when the inserted position is not the first node), that is, the head value is changed, and the root node is changed after the result is returned, is the linked list structure damaged? Or is the book incorrect? But it turns out that the linked list is not broken (see the test program ).
Finally, the key to finding and solving the problem on the internet is "linkp = & Current-> link;", that is, when you use a traversal chain table, linkp changes accordingly (when the inserted position is not the first node), it no longer points to the root node, and the last value is not the head value, what changed is the link value of the node pointed to by linkp at this time, so the final program is correct. Thank you, Gong Yuxuan.★Xiao pan, da Kui/ty.
The key to the program is Pointer operations, which are easier to understand. Pointers are indeed a major difficulty of C, but they are also a major feature of C.
It was almost a week between the previous text and the text after debugging, and the middle was intermittently tuned. I wanted to give up the program, I didn't look down on "C and pointer". I finally passed debugging in the next day. I am in a good mood and hope it will be useful to students studying C.

, 2010/5/5

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.