嵌入式第二十六篇——数据结构双向链表

一、基本概念

双向链表是一种常见的数据结构,每个节点包含两个指针,分别指向前驱节点和后继节点。与单向链表相比,双向链表可以从任意节点向前或向后遍历,操作更灵活。

二、节点定义

在C语言中,双向链表的节点通常定义为结构体,包含数据域和两个指针域:

c 复制代码
typedef struct Node {
    int data;           // 数据域
    struct Node *prev;  // 指向前驱节点
    struct Node *next;  // 指向后继节点
} Node;

三、创建双向链表

初始化一个双向链表通常需要创建头节点,并将指针初始化为NULL

c 复制代码
Node* createList() {
    Node *head = (Node*)malloc(sizeof(Node));
    if (head == NULL) {
        printf("内存分配失败\n");
        exit(1);
    }
    head->prev = NULL;
    head->next = NULL;
    return head;
}

四、插入节点

双向链表的插入操作分为头部插入、尾部插入和指定位置插入。

1、头部插入:

c 复制代码
void insertAtHead(Node *head, int data) {
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = head->next;

    if (head->next != NULL) {
        head->next->prev = newNode;
    }
    head->next = newNode;
}

2、尾部插入:

c 复制代码
void insertAtTail(Node *head, int data) {
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;

    Node *current = head;
    while (current->next != NULL) {
        current = current->next;
    }

    current->next = newNode;
    newNode->prev = current;
}

五、删除节点

删除操作需要调整前驱和后继节点的指针:

c 复制代码
void deleteNode(Node *head, int data) {
    Node *current = head->next;
    while (current != NULL) {
        if (current->data == data) {
            if (current->prev != NULL) {
                current->prev->next = current->next;
            }
            if (current->next != NULL) {
                current->next->prev = current->prev;
            }
            free(current);
            return;
        }
        current = current->next;
    }
    printf("未找到该节点\n");
}

六、遍历双向链表

双向链表支持正向和反向遍历:

1、正向遍历:

c 复制代码
void traverseForward(Node *head) {
    Node *current = head->next;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

2、反向遍历:

c 复制代码
void traverseBackward(Node *head) {
    Node *current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    while (current != head) {
        printf("%d ", current->data);
        current = current->prev;
    }
    printf("\n");
}
相关推荐
一尘之中16 小时前
从C语言底层设计到系统架构评估:软件架构知识体系全景
学习·系统架构·ai写作
_日拱一卒17 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
星夜夏空9918 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb
不羁的木木18 小时前
ArkWeb实战学习笔记05-综合实战:构建混合应用
笔记·学习·harmonyos
橙橙笔记18 小时前
Python的学习第一部分
python·学习
bush418 小时前
嵌入式linux学习记录二
linux·运维·学习
Bluetooth73019 小时前
c语言一维数组
c语言
2401_8685347820 小时前
【无标题】
数据结构·r语言
Mr. zhihao20 小时前
Redis五大高级数据结构:原理-场景-底层-横向对比
数据结构·redis
元气少女小圆丶20 小时前
SenseGlove Nova 2+Unity开发笔记1
笔记·学习·unity