Explain the basic operation of double linked list (C language)

Time:2020-12-23

@

1. Definition of bidirectional linked list

In the previous section, you learned about one-way linked listsDetailed explanation of single chain list。 Today, learn double linked list. Before learning, I will review one-way list and two-way list.


Characteristics of unidirectional linked list

We can easily get to the next node, but it is difficult to go back to the previous node

  2. Can only traverse from the beginning to the end or from the end to the end


Features of bidirectional linked list

Each time a node is inserted or deleted, it needs to handle four node references instead of two. It is more difficult to implement

   2. Compared with the unidirectional linked list, it must occupy more memory space

It can be traversed from the beginning to the end and from the end to the end


Definition of bidirectional linked list:

Bi directional linked list is also called double linked list. It is a kind of linked list. There are two pointers in each data node, which point to direct successor and direct precursor respectively. Therefore, starting from any node in the bidirectional linked list, you can easily access its predecessor node and successor node. The following figure shows the structure of the bidirectional linked list.


Insert picture description here

   as can be seen from the above, each node in the bidirectional linked list contains the following three parts of information:


  Pointer field: used to point to the direct precursor node of the current node;


  Data field: used to store data elements.


  Pointer field: used to point to the immediate successor node of the current node;


Insert picture description here


The definition of bidirectional circular linked list:

   bidirectional linked list can also be connected from the beginning to the end to form a bidirectional circular list, as shown in the figure below

When creating a linked list, you only need to connect the ending at the end (it has been marked in the code for creating the linked list). Other code can be changed slightly.


Insert picture description here

The node structure of double linked list is realized by C language

/*The range of random numbers*/

2. Creation of double linked list

   compared with the single linked list, the double linked list only has one more pointer field for each node to point to the direct precursor. Therefore, we can easily create double linked list on the basis of single linked list.

   it should be noted that, unlike the single linked list, each new node created in the double linked list must establish two links with its precursor node, namely:

    point the prior pointer of the new node to the direct precursor node;

   point the next pointer of the direct precursor node to the new node;

   here is the C language implementation code for creating a bidirectional linked list

Node* CreatList(Node * head,int length)
{
    if (length == 1)
    {
        
        return( head = CreatNode(head));
    }
    else
    {
        head = CreatNode(head);
        Node * list=head;
        for (int i=1; ipre=NULL;

3. Insert double linked list

   according to the position of data added to the bidirectional linked list, it can be subdivided into the following three situations:


1. Add to header

   to add a new data element to the header, you only need to establish a two-level logical relationship between the element and the header element.

   in other words, assuming that the new element node is temp and the header node is head, you need to do the following two steps:


  temp->next=head; head->prior=temp;

   move the head to temp and point to the new header again;

   add a new element 7 to the header of the double linked list, and the implementation process is as follows:


Insert picture description here


2. Add to the middle of the table

   similar to adding data to a single linked list, adding data in the middle of a bidirectional linked list requires the following two steps, as shown in the following figure:

   the new node first establishes a two-level logical relationship with its immediate successor node;

   the direct precursor node of the new node establishes a two-layer logical relationship with it;


Insert picture description here


3. Add to the end of the table

   is the same as adding to the header. The implementation process is as follows:

    find the last node in the double linked list;

   let the new node and the last node have a two-level logical relationship;


Insert picture description here

/*Insert the data node before the add position*/next;next;

4. Deletion of double linked list

When deleting a node in a double linked list, you only need to traverse the list to find the node to be deleted, and then remove the node from the list.

   for example, the operation process of deleting element 2 is shown in the figure:


Insert picture description here

Node * DeleteList(Node * head,int data)

5. Change node data in bidirectional linked list

The operation of changing the specified node data field in the double linked list is completed on the basis of searching. The implementation process is: find the node that stores the data element by traversing, and change its data field directly.

/*Update function, where add represents the position of the change node in the double linked list, and newelement is the value of the new data*/next;
    }
    temp->data=newElem;
    return p;
}

6. Search of double linked list

In general, double linked lists, like single linked lists, have only one header pointer. Therefore, the implementation of double linked list to find the specified element is similar to that of single linked list, in which the elements in the table are traversed successively from the header.

/*Head is the original double linked list, and element represents the element to be searched*/

7. Printing of double linked list

/*Function of output linked list*/

8. Test function and result

int main()

Insert picture description here


Everyone’s encouragement is the driving force for me to continue to create. If I feel that the writing is good, please pay attention to it, like it, collect it, forward it, thank you!


The above code is the code after testing. If there are any mistakes and mistakes, please point them out.


Some contents refer to the network, if there is infringement, please contact to delete.


If you encounter typographical problems, you can visit my CSDN through the following link.

CSDN:CSDN search for “embedded and Linux things”

Welcome to my official account: embedded and Linux, collect the autumn test written interview interview (HUAWEI millet and other big factory noodles, embedded knowledge points, written questions, resume template, etc.) and 2000G learning materials.