1.7.2、双向链表基本操作

本文详细介绍了如何在双链表中添加元素到表头、中间位置和表尾,以及删除节点、查找特定元素和更改指定位置节点的数据。通过示例代码展示了双链表的增删查改操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加节点

(1)添加至表头

  • 将新数据元素添加到表头只需要将该元素与表头元素建立双层逻辑关系即可。
  • 假设新元素节点为 temp,表头节点为 head,则需要做以下 2 步操作:
    • temp->next=head; head->prior=temp;
    • 将 head 移至 temp,重新指向新的表头
  • 例如,将新元素 7 添加至双链表的表头:

(2)添加至表的中间位置

  • 同单链表添加数据类似,双向链表中间位置添加数据需要经过以下 2 个步骤:
    • 新节点先与其直接后继节点建立双层逻辑关系
    • 新节点的直接前驱节点与之建立双层逻辑关系

(3)添加至表尾

  • 与添加到表头是一个道理,实现过程如下:
    • 找到双链表中最后一个节点
    • 让新节点与最后一个节点进行双层逻辑关系

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;
}

2、删除节点

  • 双链表删除结点时,只需遍历链表找到要删除的结点,然后将该节点从表中摘除即可。

// 删除结点的函数, 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;
}

3、查找节点

  • 通常,双向链表同单链表一样,都仅有一个头指针。因此,双链表查找指定元素的实现同单链表类似,都是从表头依次遍历表中元素。
// 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;
}

4、更改节点

  • 更改双链表中指定结点数据域的操作是在查找的基础上完成的。实现过程:通过遍历找到存储有该数据元素的结点,直接更改其数据域即可。
// 更新函数, 其中, 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;
}

5、总结:增删查改

#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;
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融码一生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值