www.cnblogs.com/jamesvoid/p…

Those of you who have studied linked lists know that there are two ways to insert elements into a linked list:

Header interpolation: data inserted into the linked list, as the first element of the list; Tail interpolation: data inserted into the linked list, as the last element of the list;

The focus of this blog post is on why we have headers. For the concept of headers and Pointers, please refer to the concept of headers and Pointers in linked lists

To insert and delete operations on the first node and other nodes, there is no complete and straightforward code explanation for this point. My previous understanding is not very clear. Here is a code to verify this: talk is cheap,show me the code

#include<stdio.h>
#include<stdlib.h>struct Node { int data; struct Node *next; }Node; typedef struct LinkedList { struct Node *head; // header pointer}LinkedList; Void Init_List(LinkedList *list) {list->head = NULL; Void Init_List_With_Head_Node(LinkedList *list) {struct Node* Node = (struct Node*)malloc(sizeof(struct Node*)) Node)); node->next = NULL; list->head = node; Void Head_Insert_List(LinkedList *list,int data) {struct Node * Node = (struct Node) Node*)malloc(sizeof(struct Node)); node->data = data; node->next = list->head; list->head = node; Void Tail_Insert_List(LinkedList *list,int data) {struct Node * Node = (struct Node * Node) Node*)malloc(sizeof(struct Node)); struct Node *tmp = list->head; node->data = data; node->next = NULL;if(TMP) {// go to the endwhile(tmp->next)
        {
            tmp = tmp->next;
        }
        tmp->next = node;
    }
    else{// head is NULL list->head = node; Void Head_Insert_List_With_Head_Node(LinkedList *list,int data) {struct Node * Node = (struct Node) Node*)malloc(sizeof(struct Node)); node->data = data; node->next = list->head->next; list->head->next = node; Void Tail_Insert_List_With_Head_Node(LinkedList *list,int data) {struct Node * Node = (struct Node * Node) Node*)malloc(sizeof(struct Node)); struct Node *tmp = list->head; node->data = data; node->next = NULL; // go to the tailwhile(tmp->next)
    {
        tmp = tmp->next;
    }
    
    tmp->next = node;
}

void print_list(LinkedList *list)
{
    int i;
    struct Node *tmp = list->head;
    while(tmp)
    {
        printf("%d\t",tmp->data);
        tmp = tmp->next;
    }
    puts("");
}

int main()
{
    LinkedList list;

    puts("Headless :");
    Init_List(&list);

    puts("Head insertion");
    Head_Insert_List(&list,1);
    Head_Insert_List(&list,2);
    Head_Insert_List(&list,3);
    print_list(&list);

    puts("Tail insertion");
    Init_List(&list);
    Tail_Insert_List(&list,1);
    Tail_Insert_List(&list,2);
    Tail_Insert_List(&list,3);
    print_list(&list);

    puts("Has a header :");
    Init_List_With_Head_Node(&list);

    puts("Head insertion");
    Head_Insert_List_With_Head_Node(&list,1);
    Head_Insert_List_With_Head_Node(&list,2);
    Head_Insert_List_With_Head_Node(&list,3);
    print_list(&list);

    puts("Tail insertion");
    Init_List_With_Head_Node(&list);
    Tail_Insert_List_With_Head_Node(&list,1);
    Tail_Insert_List_With_Head_Node(&list,2);
    Tail_Insert_List_With_Head_Node(&list,3);
    print_list(&list);

    return 0;
}Copy the code

The result is as follows:

The number circled in red above is garbage in memory because the data from the head node was not initialized.

A list with no leading node because the head pointer points to the first element node:

  1. If there is no element in the current list, it is necessary to update the header pointer to the current added element.
  2. When deleting an element, you need to determine whether the deleted element is the first element. If it is the first element, you need to update the header pointer to the next element.

Because the head pointer points to this head node, the head pointer will not be NULL. There is no special consideration as described above. The operation on the first element is the same as that on other elements.

Let’s go change the world,or changed by the world