C Language: Linked list __c language

Source: Internet
Author: User

I record technology video address: https://edu.csdn.net/lecturer/1899 Welcome to watch.

first, the basic concept of the linked list

This section explains the list in the C language, and only explains the single linked list. In a single linked list, each node contains a pointer to the next node in the list. The value of the pointer field at the last node of the list is null, suggesting that there are no more nodes after the list. Once you have found the first node of the list, the pointer can take you to all the remaining nodes. To remember the starting position of the linked list, you can use the root pointer (root pointer). The root pointer points to the first node in the linked list. The root pointer is just a pointer, and it does not contain any data. The schematic diagram is as follows:


So, we can create the corresponding structure according to the structure of the linked list:

typedef struct Node {
    struct node *link;
    int value;
} Node;
1. The address of the next node is saved in each node and stored with *link.

2. Each node has its own value, stored in value.

In the diagram above, these nodes are next to each other in order to display the logical order provided by the linked list. In fact, the nodes in a linked list may be distributed in various places in memory. It does not matter whether the nodes are physically contiguous, because the program always moves from one node to another with a chain (pointer).


second, the single linked list of the insertion

Let's say we have a new value of 12 and want to insert it into the list above. The idea is very clear, starting at the start of the list, following the pointer until you find the first node with a value greater than 12, and then inserting the new value into the position before that node. However, the pointer field of the previous node must be modified to implement this insertion, but we have crossed the previous node and cannot go back. So we can always save a pointer to the node that is between the current node of the list.

So the analysis shows that the code needs three pointer variables to point to different nodes.

1. Node *current to point to the current node.

2. Node *previous, which is used to point to the previous node.

3. Node *new, which points to the newly allocated node.

Below, I use a flowchart to parse the insert process.

The state before the insertion (current points to the first node):


Now the Current->value is 5, less than 12. So the previous and current pointers need to move one forward, as shown in the following figure:


Now the Current->value is 10, or less than 12. So the previous and current pointers need to move one forward, as shown in the following figure:


At this point, the value of the Current->value is 15, greater than 12. We need to create a new node, ready to insert:



Inserting this node into the list requires two steps:

1. Make the new node point to the node that will be the next node in the list, that is, the first node we found that has a value greater than 12. Code is

New->link = current;
The effect chart is as follows:

2. Let the node pointed to by the previous pointer point to the new node. Code is

Previous->link = new;
The effect chart is as follows:


Thus, the basic insert operation of a single linked list is completed, and the code is as follows:

int Insert (node *current, int new_value) {
    node *previous;
    Node *new;
    
    Find the correct insertion position by sequentially accessing the linked list until a node whose value is greater than or equal to the new value is reached. While
    (Current->value < New_value) {
        previous = current;
        Current = current->link;
    }
    
    Assigns the newly inserted node new
    = (node *) malloc (sizeof (node));
    if (new = = NULL) {//allocation failed return
        0;
    }
    New->value = New_value;
    
    Insert the new node into the list
    new->link = current;
    Previous->link = new;
    
    return 1;
}

However, the code is still flawed, and we now consider the problem of edge values.

1. The inserted value is greater than the maximum value of the value in the linked list.

2. The inserted value is less than the minimum value of the value in the linked list.

First of all, let's say that the insertion value is 20. While loop execution to the end, the Current->value value is 15, executing current = current->link; , current is null, and then tries to execute the judgment in the while statement, and when current is null, the execution current->value the error. So we need to limit the conditions of judgment. The code is as follows:

while (current!= NULL && Current->value < New_value)
Thus, the flowchart after inserting 20 is as follows:




To analyze the second case, suppose the inserted value is 3. The condition of the while loop is never set, because the current pointer defaults to the first node, and we cannot manipulate root, which is to not manipulate the root pointer.

To insert a node at the start of the list, the function must modify the root pointer. So we need to define a pointer to the root pointer. So this pointer is a pointer to a pointer. We can define Node **ROOTP;

So we can modify the code as follows:

int Insert (node **ROOTP, int new_value) {
    node *current;
    Node *previous;
    Node *new;
    Gets the pointer to the first node, current
    = *ROOTP;
    previous = NULL;
    
    Find the correct insertion position by sequentially accessing the linked list until a node whose value is greater than or equal to the new value is reached. While (the current
    != NULL && current->value < New_value) {
        previous = current;
        Current = current->link;
    }
    
    Assigns the newly inserted node new
    = (node *) malloc (sizeof (node));
    if (new = = NULL) {//allocation failed return
        0;
    }
    New->value = New_value;
    
    Insert the new node into the list
    new->link = current;
    if (previous = = NULL) {
        *ROOTP = new;
    } else {
        previous->link = new;
    }
    
    return 1;
}

After modifying the code, the initial flowchart is as follows:



After inserting this node with a value of 3, the flowchart is as follows:



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.