当前位置: 首页 > news >正文

数据结构基础(带头节点的双向循环链表)

完整代码

      • DLinkList.h
      • DLinkList.c
      • test.c

DLinkList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>typedef int ElemType;// SList - 单链表
// DList - 双链表
// 带头节点的双向循环链表 - 最优链表结构,任意位置插入、删除数据,时间复杂度O(1)
typedef struct ListNode{struct ListNode* prev;struct ListNode* next;ElemType data;
}ListNode;// 初始化
ListNode* ListInit();
// 打印
void ListPrint(ListNode* phead);
// 销毁
void ListDestory(ListNode* phead);
// 尾插
void ListPushBack(ListNode* phead, ElemType x);
// 头产
void ListPushFront(ListNode* phead, ElemType x);
// 尾删
void ListPopBack(ListNode* phead);
// 头删
void ListPopFront(ListNode* phead);// 查找节点的pos位置
ListNode* ListFind(ListNode* phead, ElemType x);
// 在pos位置前插入
void ListInsert(ListNode* pos, ElemType x);
// 删除pos位置的值
void ListErase(ListNode* pos);

DLinkList.c

#define _CRT_SECURE_NO_WARNINGS 1#include "DList.h"// 返回节点(头节点、要插入的节点)
ListNode* BuyListNode(ElemType x)
{ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));newNode->data = x;newNode->prev = NULL;newNode->next = NULL;return newNode;
}
// 初始化
ListNode* ListInit()
{ListNode* phead = BuyListNode(0);// 申请头节点,数据域传0phead->next = phead;phead->prev = phead;return phead;
}
// 打印
void ListPrint(ListNode* phead)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){printf("%d ", cur->data);cur = cur->next;}printf("\n");
}
// 销毁
void ListDestory(ListNode* phead)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){ListNode* next = cur->next;// 先找到要删节点的下一个节点free(cur);cur = next;}free(phead);phead = NULL;
}
// 尾插(直接调用Insert(在pos位置前插入))
void ListPushBack(ListNode* phead, ElemType x)
{assert(phead);// 头节点不为空//ListNode* newNode = BuyListNode(x); 带头的双向循环链表不考虑没有节点,有节点的情况//ListNode* tail = phead->prev;// 定义一个尾指针,指向头节点的前驱 画图分析//tail->next = newNode;//newNode->prev = tail;//newNode->next = phead;//phead->prev = newNode;ListInsert(phead, x);}
// 头插(直接调用Insert(在pos位置前插入))
void ListPushFront(ListNode* phead, ElemType x)
{assert(phead); 没有节点也不会有问题//ListNode* first = phead->next;//ListNode* newNode = BuyListNode(x);//phead->next = newNode;//newNode->prev = phead;//newNode->next = first;//first->prev = newNode;ListInsert(phead->next, x);}// 头删(可以调用Erase)
void ListPopFront(ListNode* phead)
{//assert(phead);//assert(phead->next != phead);// 链表没有一个节点//ListNode* first = phead->next;//ListNode* second = first->next;//phead->next = second;//second->prev = phead;//free(first);//first = NULL;ListErase(phead->next);
}
// 尾删(可以调用Erase)
void ListPopBack(ListNode* phead)
{//assert(phead);//assert(phead->next != phead);// 链表没有一个节点//ListNode* tail = phead->prev;//ListNode* prev = tail->prev;//prev->next = phead;//phead->prev = prev;//free(tail);//tail = NULL;ListErase(phead->prev);
}// 查找节点的pos位置
ListNode* ListFind(ListNode* phead, ElemType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->data == x)return cur;cur = cur->next;}return NULL;
}
// 在pos位置(前!!!)插入
void ListInsert(ListNode* pos, ElemType x)
{assert(pos);ListNode* prev = pos->prev;ListNode* newNode = BuyListNode(x);prev->next = newNode;newNode->prev = prev;newNode->next = pos;pos->prev = newNode;
}
// 删除pos位置的值
void ListErase(ListNode* pos)
{ListNode* prev = pos->prev;ListNode* next = pos->next;prev->next = next;next->prev = prev;free(pos);pos = NULL;}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "DList.h"void testList1()
{ListNode* plist = ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);ListPushFront(plist, 0);ListPushFront(plist, -1);ListPrint(plist);ListPopFront(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListDestory(plist);
}
void testList2()
{ListNode* plist = ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);ListNode* pos = ListFind(plist, 2);if (pos){pos->data *= 10;printf("找到了,并且乘10\n");}elseprintf("没有找到\n");ListPrint(plist);ListInsert(pos, 300);ListPrint(plist);ListErase(pos);ListPrint(plist);
}
int main()
{testList1();return 0;
}
http://www.lryc.cn/news/250327.html

相关文章:

  • STM32CubeMx+MATLAB Simulink点灯程序
  • 【深度学习】gan网络原理生成对抗网络
  • springboot参数汇总
  • 【算法刷题】Day9
  • LangChain的函数,工具和代理(三):LangChain中轻松实现OpenAI函数调用
  • WiFi概念介绍
  • 如何优雅的进行业务分层
  • C++的std命名空间
  • unity学习笔记
  • 使用SpringBoot和ZXing实现二维码生成与解析
  • C++模板—函数模板、类模板
  • Monkey
  • SQL中left join、right join、inner join等的区别
  • 算法学习—排序
  • 在Pycharm中创建项目新环境,安装Pytorch
  • linux里source、sh、bash、./有什么区别
  • IDEA编译器技巧-提示词忽略大小写
  • 【MySQL】MySQL安装 环境初始化
  • C# IList 与List区别二叉树的层序遍历
  • 助力android面试2024【面试题合集】
  • 【动态规划】LeetCode-62.不同路径
  • 对 Vision Transformers 及其基于 CNN-Transformer 的变体的综述
  • MongoDB简介
  • 尚硅谷hadoop3.x课程部分资料文件下载,jdk,hadoopjar包
  • vue el-radio-group多选封装及使用
  • Kaggle-水果图像分类银奖项目 pytorch Densenet GoogleNet ResNet101 VGG19
  • TPLink-Wr702N 通过OpenWrt系统打造打印服务器实现无线打印
  • [UGUI]实现从一个道具栏拖拽一个UI道具到另一个道具栏
  • 微服务--08--Seata XA模式 AT模式
  • Doris 数据导入一:Broker Load 方式