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.