Data structure and algorithm - basic operation of doubly linked list (C language implementation)

We have learned how to create a doubly linked list, and now we will learn some basic operations about the doubly linked list, that is, how to add, delete, find or change data elements in the doubly linked list.

The knowledge in this section is based on the proficiency in the creation process of the doubly linked list. We continue to learn the content of this section by continuing the doubly linked list created in the previous section. The created doubly linked list is shown in the following figure:

Schematic diagram of a doubly linked list

Doubly linked list add node

According to the position where the data is added to the doubly linked list, it can be subdivided into the following three situations:

add to header

To add a new data element to the header, it is only necessary to establish a two-layer 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:

  1. temp->next=head; head->prior=temp;
  2. Move the head to temp and point to the new header again;

For example, to add a new element 7 to the header of the doubly linked list, the implementation process is shown in the following figure:

Add elements to the header of the doubly linked list

Add to the middle of the table

Similar to adding data in a singly linked list, adding data in the middle of a doubly linked list requires the following two steps, as shown in the following figure:

  1. The new node first establishes a two-level logical relationship with its direct successor node;
  2. The direct predecessor node of the new node establishes a two-level logical relationship with it;

Add data elements in the middle of the doubly linked list

Add to footer

It is the same as adding to the table header. The implementation process is as follows (as shown in the figure below):

  1. Find the last node in the doubly linked list;
  2. Let the new node have a double-layer logical relationship with the last node;

Add data elements at the end of the doubly linked list

Therefore, we can try to write the C language code for adding data to the doubly linked list. The reference code is as follows:

line * insertLine(line * head,int data,int add){
    //新建数据域为data的结点
    line * temp=(line*)malloc(sizeof(line));
    temp->data=data;
    temp->prior=NULL;
    temp->next=NULL;
    //插入到链表头,要特殊考虑
    if (add==1) {
        temp->next=head;
        head->prior=temp;
        head=temp;
    }else{
        line * body=head;
        //找到要插入位置的前一个结点
        for (int i=1; i<add-1; i++) {
            body=body->next;
        }
        //判断条件为真,说明插入位置为链表尾
        if (body->next==NULL) {
            body->next=temp;
            temp->prior=body;
        }else{
            body->next->prior=temp;
            temp->next=body->next;
            body->next=temp;
            temp->prior=body;
        }
    }
    return head;
}

Doubly linked list delete node

When deleting a node in a double linked list, you only need to traverse the linked 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 from the base of the first diagram is shown in the following diagram:

Schematic diagram of the operation of deleting elements in a double linked list

The C language implementation code for deleting nodes in the doubly linked list is as follows:

//删除结点的函数,data为要删除结点的数据域的值
line * delLine(line * head,int data){
    line * temp=head;
    //遍历链表
    while (temp) {
        //判断当前结点中数据域和data是否相等,若相等,摘除该结点
        if (temp->data==data) {
            temp->prior->next=temp->next;
            temp->next->prior=temp->prior;
            free(temp);
            return head;
        }
        temp=temp->next;
    }
    printf("链表中无该数据元素");
    return head;
}

doubly linked list lookup node

Usually, a doubly linked list, like a singly linked list, has only one head pointer. Therefore, the implementation of searching for a specified element in a double-linked list is similar to that of a single-linked list, traversing the elements in the table sequentially from the header.

The C language implementation code is:

//head为原双链表,elem表示被查找元素
int selectElem(line * head,int elem){
//新建一个指针t,初始化为头指针 head
    line * t=head;
    int i=1;
    while (t) {
        if (t->data==elem) {
            return i;
        }
        i++;
        t=t->next;
    }
    //程序执行至此处,表示查找失败
    return -1;
}

doubly linked list change node

The operation of changing the data field of the specified node in the doubly linked list is done on the basis of searching. The implementation process is: find the node storing the data element through traversal, and directly change its data field.

The C language implementation code to realize this operation is as follows:

//更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
line *amendElem(line * p,int add,int newElem){
    line * temp=p;
    //遍历到被删除结点
    for (int i=1; i<add; i++) {
        temp=temp->next;
    }
    temp->data=newElem;
    return p;
}

Summarize

Here is the complete implementation code for "addition, deletion, checking and modifying" operations on data in the double-linked list:

#include <stdio.h>
#include <stdlib.h>
typedef struct line{
    struct line * prior;
    int data;
    struct line * next;
}line;
//双链表的创建
line* initLine(line * head);
//双链表插入元素,add表示插入位置
line * insertLine(line * head,int data,int add);
//双链表删除指定元素
line * delLine(line * head,int data);
//双链表中查找指定元素
int selectElem(line * head,int elem);
//双链表中更改指定位置节点中存储的数据,add表示更改位置
line *amendElem(line * p,int add,int newElem);
//输出双链表的实现函数
void display(line * head);
int main() {
    line * head=NULL;
    //创建双链表
    head=initLine(head);
    display(head);
    //在表中第 3 的位置插入元素 7
    head=insertLine(head, 7, 3);
    display(head);
    //表中删除元素 2
    head=delLine(head, 2);
    display(head);
    printf("元素 3 的位置是:%d\n",selectElem(head,3));
    //表中第 3 个节点中的数据改为存储 6
    head = amendElem(head,3,6);
    display(head);
    return 0;
}
line* initLine(line * head){
    head=(line*)malloc(sizeof(line));
    head->prior=NULL;
    head->next=NULL;
    head->data=1;
    line * list=head;
    for (int i=2; i<=5; i++) {
        line * body=(line*)malloc(sizeof(line));
        body->prior=NULL;
        body->next=NULL;
        body->data=i;
        list->next=body;
        body->prior=list;
        list=list->next;
    }
    return head;
}
line * insertLine(line * head,int data,int add){
    //新建数据域为data的结点
    line * temp=(line*)malloc(sizeof(line));
    temp->data=data;
    temp->prior=NULL;
    temp->next=NULL;
    //插入到链表头,要特殊考虑
    if (add==1) {
        temp->next=head;
        head->prior=temp;
        head=temp;
    }else{
        line * body=head;
        //找到要插入位置的前一个结点
        for (int i=1; i<add-1; i++) {
            body=body->next;
        }
        //判断条件为真,说明插入位置为链表尾
        if (body->next==NULL) {
            body->next=temp;
            temp->prior=body;
        }else{
            body->next->prior=temp;
            temp->next=body->next;
            body->next=temp;
            temp->prior=body;
        }
    }
    return head;
}
line * delLine(line * head,int data){
    line * temp=head;
    //遍历链表
    while (temp) {
        //判断当前结点中数据域和data是否相等,若相等,摘除该结点
        if (temp->data==data) {
            temp->prior->next=temp->next;
            temp->next->prior=temp->prior;
            free(temp);
            return head;
        }
        temp=temp->next;
    }
    printf("链表中无该数据元素");
    return head;
}
//head为原双链表,elem表示被查找元素
int selectElem(line * head,int elem){
//新建一个指针t,初始化为头指针 head
    line * t=head;
    int i=1;
    while (t) {
        if (t->data==elem) {
            return i;
        }
        i++;
        t=t->next;
    }
    //程序执行至此处,表示查找失败
    return -1;
}
//更新函数,其中,add 表示更改结点在双链表中的位置,newElem 为新数据的值
line *amendElem(line * p,int add,int newElem){
    line * temp=p;
    //遍历到被删除结点
    for (int i=1; i<add; i++) {
        temp=temp->next;
    }
    temp->data=newElem;
    return p;
}
//输出链表的功能函数
void display(line * head){
    line * temp=head;
    while (temp) {
        if (temp->next==NULL) {
            printf("%d\n",temp->data);
        }else{
            printf("%d->",temp->data);
        }
        temp=temp->next;
    }
}

The program execution result is:

1->2->3->4->5
1->2->7->3->4->5
1->7->3->4->5
元素 3 的位置是:3
1->7->6->4->5

2023 new version of data structure and algorithm Java video tutorial (Part 1), data structure and algorithm that senior java programmers must learn
2023 new version of data structure and algorithm Java video tutorial (part 2), data structure and algorithm that java senior programmer must learn

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/131822673