C++实现双链表的创建、插入、删除、查询、遍历、销毁等基本操作

C++实现双链表的创建、插入、删除、查询、遍历、销毁等基本操作

1.双链表:每个节点(首尾节点除外)都包含指向后继和前驱指针,从某节点出发可向前(后)遍历。

2.代码

#include <iostream>
#include <malloc.h>

using namespace std;
// 双链表的实现
typedef int ElementType;
// 结构体定义
typedef struct DNode{
    ElementType data;
    struct DNode *prior;// 前驱
    struct DNode *next; // 后继
} DNode, *DNodeList;
// 函数声明
DNodeList Create(ElementType e);
bool Insert(DNodeList &L, int pos, ElementType e);
DNodeList Delete(DNodeList &L, int pos);
int SearchByData(DNodeList &L, ElementType e);
bool IsEmpty(DNodeList &L);
bool CheckPos(DNodeList &L, int pos);
int GetLen(DNodeList &L);
bool Destroy(DNodeList &L);
void Print(DNodeList &L);
//创建
DNodeList Create(ElementType e){
    DNodeList ans = (DNodeList) malloc(sizeof(DNode));
    if (ans->data == NULL) return NULL;
    ans->data = e;
    ans->prior = NULL;
    ans->next = NULL;
    return ans;
}
//插入
bool Insert(DNodeList &L, int pos, ElementType e){
    // 检查位置
    if (!CheckPos(L, pos)) return false;
    // 申请节点
    DNodeList n = Create(e);
    if (n == NULL) return false;
    DNodeList cur = L, prior; // 遍历指针、前驱指针
    // 寻找插入位置
    while(cur != NULL && pos-- > 0){
        prior = cur;
        cur = cur->next;
    }
    if (cur != NULL){ // 非尾节点
        cur->prior = n; // 当前指针的前驱指针指向新指针
    }
    n->next = cur;
    n->prior = prior; // 新节点的前驱指针指向新节点
    prior->next = n;
    return true;
}
//删除
DNodeList Delete(DNodeList &L, int pos){
    if (!CheckPos(L, pos)) return NULL;
    DNodeList cur = L, prior;
    // 寻找删除位置
    while(cur != NULL && pos-- > 0){
        prior = cur;
        cur = cur->next;
    }
    if (cur == NULL) return NULL;
    if (cur->next != NULL){
        cur->next->prior = prior;
    }
    prior->next = cur->next; // 跳过当前节点指向下一节点
    free(cur);
    return cur;
}
//查询
int SearchByData(DNodeList &L, ElementType e){
    DNodeList cur = L;
    int pos = 1;
    while(cur != NULL){
        cur = cur->next;
        if (cur->data == e) return pos;
        pos++;
    }
    return -1;
}
//判空
bool IsEmpty(DNodeList &L){
    return L->next == NULL;
}
//检查位置是否合法
bool CheckPos(DNodeList &L, int pos){
    return pos > 0 || pos <= GetLen(L) + 1;
}
//获取长度
int GetLen(DNodeList &L){
    int len = 0;
    DNodeList cur = L;
    while(cur->next != NULL){
        len ++;
        cur = cur->next;
    }
    return len;
}
//销毁
bool Destroy(DNodeList &L){
    DNodeList cur = L;
    while(cur != NULL){
        // 每次都从当前指针删除第1个元素
        if(Delete(cur, 1) == NULL) return false;
        cur = cur->next;
    }
    L = NULL;
    free(L);
    return true;
}
//打印
void Print(DNodeList &L){
    printf("\n");
    DNodeList cur = L;
    while(cur->next != NULL){
        cur = cur->next;
        printf("%d ", cur->data);
    }
    printf("\n");
}
int main()
{
    DNodeList L = Create(0);
    if (L == NULL) printf("创建失败");
    else printf("创建成功");
    for(int i = 0;i < 10; i++){
        Insert(L, i + 1, i + 1);
    }
    Print(L);
    DNodeList del = Delete(L, 3);
    if (del == NULL) printf("删除位置错误");
    else {
        printf("删除元素成功");
        Print(L);
    }
    int e = 4, pos = SearchByData(L, e);
    if (pos != -1) printf("元素%d位于:%d", e, pos);
    else printf("未查找到元素%d", e);
    printf("\n");
    if (Destroy(L)) printf("已销毁");
    else {
        printf("销毁失败");
        Print(L);
    }
    return 0;
}

3.效果

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是基于双向链表插入数据并自动排序的 C 代码示例: ```c #include <stdio.h> #include <stdlib.h> /* 双向链表结构体 */ typedef struct node { int data; struct node *next; struct node *prev; } Node; /* 插入数据并自动排序 */ void insert(Node **head, int data) { /* 创建新节点 */ Node *new_node = (Node*) malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; new_node->prev = NULL; /* 如果链表为空,则新节点为头节点 */ if (*head == NULL) { *head = new_node; return; } /* 头节点数据比新节点数据大,新节点插入头部 */ if ((*head)->data > data) { new_node->next = *head; (*head)->prev = new_node; *head = new_node; return; } /* 查找新节点插入位置 */ Node *cur = *head; while (cur->next != NULL && cur->next->data < data) { cur = cur->next; } /* 插入新节点 */ new_node->next = cur->next; new_node->prev = cur; if (cur->next != NULL) { cur->next->prev = new_node; } cur->next = new_node; } /* 打印链表 */ void print_list(Node *head) { Node *cur = head; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } printf("\n"); } /* 主函数 */ int main() { Node *head = NULL; insert(&head, 3); insert(&head, 1); insert(&head, 4); insert(&head, 2); insert(&head, 5); print_list(head); return 0; } ``` 在上述代码中,我们首先定义了双向链表的结构体,包括数据域 `data` 和指向前驱节点和后继节点的指针 `prev` 和 `next`。 然后,我们定义了一个 `insert` 函数来实现插入数据并自动排序。如果链表为空,则新节点为头节点;如果头节点数据比新节点数据大,则新节点插入头部;否则,我们遍链表找到新节点的插入位置,并插入新节点。 最后,我们实现了一个 `print_list` 函数来打印链表,并在主函数中测试了我们的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Qian_Qian_IT

感谢您的赏识,我将持续创作~

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

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

打赏作者

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

抵扣说明:

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

余额充值