Detailed list (C language)

https://www.cnblogs.com/ariel-dreamland/p/10469696.html
list is a common basic data structures , a structure pointer here has been fully utilized.

Lists can dynamically be storage allocation , that is to say, a linked list is extremely powerful array , he can define multiple data types in a node , also free to add as needed, delete, insert node.

Chain has a head pointer , generally head represented, is stored in an address . Linked list nodes are divided into two categories , the first node and the general nodes , the first node is no data field is. Each node in the linked list into two parts, a data field, a pointer field .

Here you should understand, the list just like a chain, like car, head pointing to the first element: the first element and the second element points; ...... until the last element that no longer point to other elements, it is called "footer" part of its address to put a "NULL" (indicating "empty address"), the list ends.

As the list with a powerful, of course there are many of his operations, such as: the list to create, modify, delete, insert, export, sort, in reverse order, clear the list of elements, find the length of the list, and so on.

Create a list:

typedef struct Student {
     int Score;
     struct Student * Next; 
} LinkList; (note semicolon)

Generally we are used to create a list typedef struct, because the definition of this structure variables, we can directly be used LinkList * a; definition of the structure type variables.

To Learn More:

typedef is a keyword in C language, the role is to define a new name for a data type. Here the type of data include an internal data type (int, char, etc.) and custom data type (struct and the like).

Typedef use in general purpose programming there are two, one for variable a clear and easy to remember name meaning new, and the other is to simplify some of the more complex type declaration.

The first:

typedef struct student{
    int score;
    struct student *next;
} LinkList;

This statement is actually completed two actions:

(1) define a new type of structure:

typedef struct student{
    int score;
    struct student *next;
} ;

(2) typedef has a name for this new structure, called LinkList.

Initializing a chain, n is the number of list node :

Copy the code
* Creat LinkList ( int n-) { 
    LinkList * head, Node *, * End; // definition of the head node, the ordinary node, tail node; 
    head = (LinkList *) the malloc ( the sizeof (LinkList)); // allocate addresses 
    end = head;          // if the list is empty as head and tail nodes 
    for ( int I = 0 ; I <n-; I ++ ) { 
        node = (LinkList *) the malloc ( the sizeof (LinkList)); 
        Scanf ( " % D " , & node-> Score); 
        End -> Next = Node; 
        End =Node; 
    } 
    End -> Next = NULL; // End Create 
    return head; 
}
Copy the code

Review list node values :

Modify the list node value is very simple. The following is a list of incoming node to be modified and to modify the function value.

Copy the code
void Change (LinkList List *, int n) { // n is the n-th node 
    LinkList = T * List;
     int I = 0 ;
     the while (I <! && n T = NULL) { 
        T = T-> Next; 
        I ++ ; 
    } 
    IF (T =! NULL) { 
        the puts ( " enter the modified value " ); 
        Scanf ( " % D " , & T-> Score); 
    } 
    the else { 
        the puts ( " node does not exist " ); 
    } 
}
Copy the code

Delete list node :

Delete the list of elements that is the pointer over the field in front of the node points to the next node to be removed the next node. That is: p-> next = q-> next; q node then releasing space, i.e. free (q).

Copy the code
void delet(LinkList *list, int n) {
    LinkList *t = list, *in;
    int i = 0;
    while (i < n && t != NULL) {
        in = t;
        t = t->next;
        i++;
    }
    if (t != NULL) {
        in->next = t->next;
        free(t);
    }
    else {
        puts("节点不存在");
    }
}
Copy the code

Insert list node :

Insertion node is inserted in the data field with the nodes on the node pointer field prior to insertion of the link, and then inserted into the insertion node after the data field on the target domain linked nodes. According to FIG., The insertion node that is: e-> next = head-> next; head-> next = e.

Increasing list node structure uses two int pointer and a data.

Copy the code
void insert(LinkList *list, int n) {
    LinkList *t = list, *in;
    int i = 0;
    while (i < n && t != NULL) {
        t = t->next;
        i++;
    }
    if (t != NULL) {
        in = (LinkList*)malloc(sizeof(LinkList));
        puts("输入要插入的值");
        scanf("%d", &in->score);
        in -> Next = T-> Next; // fill in a pointer field of the node, i.e. in the pointer field points to the next node t of 
        T-> Next = in ; // filled t pointer field of the node, the the pointer field redirected t in 
    }
     the else { 
        the puts ( " node does not exist " ); 
    } 
}
Copy the code

Output list :

Output list is very simple, while traversing the output side of the line.

while (h->next != NULL) {
        h = h->next;
        printf("%d  ", h->score);
}
Published 42 original articles · won praise 148 · views 410 000 +

Guess you like

Origin blog.csdn.net/baidu_37503452/article/details/104904322