[learning notes] the addition, deletion, modification and query of the linked list has been summarized as a classic example (implemented in C language)

Unidirectional linked list

Basic concepts

  1. The linked list is composed of nodes connected by pointers, and each node is composed of data part and pointer (non data) part.
  2. The physical storage mode of array is continuous, so you can access the array through subscript, while the physical storage mode of linked list is discontinuous, and you can only access the next node of the current node through pointer.
  3. The first node in the linked list is the header, and the last node points to NULL. (there seems to be no end of the watch.)

Basic operation of linked list

Insert operation

  1. Header insertion: after creating a node, point the node pointer to the head node of the linked list, and the head node points to the new node.
  2. Tail interpolation: after creating a node, the last node pointer in the linked list points to the new node. Note that the new node pointer should be set to NULL. (prevent NULL and wild pointers)
  3. Insert node t after the specified node: This is more complex. The operation is probably as follows: define two pointers P and Q, where p refers to the head node and Q points to the previous node of P node. Cycle through the linked list until Q finds the specified node and ends the cycle. At this time, execute t - > next = P, q - > next = p; Operation. (as shown in the figure)

  • Tip: when using the tail insertion method, you can define a variable to point to the tail node, so that each sub insertion can realize the O (1) level insertion operation without traversing the whole linked list.
  • Tip: create a virtual node that points to the head node. In this way, we don't have to deal with the deletion of the head node.
  • tips: pay attention to the order of each step. Some steps should not be confused. It is recommended to seriously think about the reasons, which is conducive to your deeper understanding and mastery of the deletion operation of the linked list.

Delete operation

To delete a specified node:

Define two pointers P and Q, where p refers to the head node and Q points to the previous node of P node. Cycle through the linked list until P finds the specified node and ends the traversal. At this time, execute q = P - > next, free (P); Operation. (as shown in the figure)

Find (modify) operation

  • Define a pointer p to the head node, cycle through the linked list until the specified node is found, and then perform the search operation (modification operation).
  • No. (it's recommended to do it by hand. Sometimes I don't know if there will be problems in my place without doing it)

Reference code

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//Define linked list nodes
typedef struct node
{
    int val;
    struct node* next;
}node;
void print_list(node*);
//Insert a new node after specifying the node location
//Return value: 1 true, 0 false
int list_insert(node* prehead, node* target, node* t)
{
    node* q = prehead, *p = q -> next;
    while(p && q != target)
    {
        q = p;
        p = p -> next;
    }
    if(q != target) return 0;
    q -> next = t;
    t -> next = p;
    //Print the linked list by the way
    print_list(prehead);
    return 1;
}

//Tail interpolation
int list_insertback(node* prehead, node* t)
{
    node* q = prehead, *p = q -> next;
    while(p)
    {
        q = p;
        p = p -> next;
    }
    q -> next = t;
    //Print the linked list by the way
    print_list(prehead);
    return 1;
}

//Delete specified node
//Return value: 1 true, 0 false
int list_delete(node* prehead, int val)
{
    node* q = prehead, *p = q -> next;
    while(p && p -> val != val)
    {
        q = p;
        p = p -> next;
    }
    if(p -> val != val) return 0;
    q -> next = p -> next;
    free(p);
    //Print the linked list by the way
    print_list(prehead);
    return 1;
}

//Print all node values of the linked list
void print_list(node* prehead)
{
    node* p = prehead -> next;
    printf("[");
    while(p)
    {
        printf("%d -> ", p -> val);
        p = p -> next;
    }
    printf("null]\n");
}

node* init_node(int val)
{
    node* t = (node*)malloc(sizeof(node));
    t -> val = val;
    t -> next = NULL;
    return t;
}

int main()
{
    srand(time(0));
    node* prehead = init_node(0);
    node* t1 = init_node(2);
    list_insertback(prehead, t1);
    node* t2 = init_node(5);
    list_insertback(prehead, t2);
    t2 = init_node(3);
    list_insert(prehead, t1, t2);
    list_delete(prehead, 2);
    list_delete(prehead, 5);
    return 0;
}

Classic examples

In the recent practice of linked list questions, Ben found that linked list questions have many interesting questions, and interviewers love this kind of questions. There is no general template for the linked list questions. I suggest you to practice more. Master the following questions, and you can fight most of the linked list questions in the school recruitment interview!

Development: two-way linked list

If you master the one-way linked list, then the two-way linked list is basically no problem. The two-way linked list has a pre pointer in the node to point to the previous node. If it is inserted or deleted, there is only one more pre pointer redirection than the one-way linked list. The advantage of bi-directional linked list is that it can find the precursor node of the node. The disadvantage is that it reduces the proportion of data in the structure and generates external overhead.

Tags: C data structure linked list

Posted on Tue, 28 Sep 2021 05:12:58 -0400 by JeremyCanada26