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

【数据结构】双链表

        带头双向循环链表。

List.h

#pragma once#include<stddef.h>//NULL
#include<stdlib.h>//free
#include<assert.h>//asserttypedef int SLDataType;typedef struct SLNode
{SLDataType data;struct SLNode* prev;struct SLNode* next;
} SLNode;SLNode* SLInit();void SLPushFront(SLNode* head, SLDataType x);
void SLPushBack(SLNode* head, SLDataType x);
SLDataType SLPopBack(SLNode* head);
SLDataType SLPopFront(SLNode* head);
SLDataType SLErase(SLNode* Del);
void SLInsertBF(SLNode* AFnode, SLDataType x);
void SLInsertAF(SLNode* AFnode, SLDataType x);
void SLDestroy(SLNode** phead);

List.c

#include"List.h"static SLNode* BuySLNode(SLDataType x)
{SLNode* newNode = (SLNode*)malloc(sizeof(SLNode));if (NULL == newNode){perror("malloc failed");exit(-1);}newNode->data = x;newNode->prev = newNode;newNode->next = newNode;return newNode;
}SLNode* SLInit()
{return BuySLNode(0);
}void SLPushFront(SLNode* head, SLDataType x)
{assert(head);//SLInsertBF(head->next, x);SLInsertAF(head, x);
}void SLPushBack(SLNode* head, SLDataType x)
{assert(head);//SLInsertBF(head, x);SLInsertAF(head->prev, x);
}SLDataType SLPopBack(SLNode* head)
{assert(head);assert(head->prev != head);return SLErase(head->prev);
}SLDataType SLPopFront(SLNode* head)
{assert(head);assert(head->next != head);return SLErase(head->next);
}SLDataType SLErase(SLNode* Del)
{assert(Del);Del->prev->next = Del->next;Del->next->prev = Del->prev;SLDataType ret = Del->data;free(Del);return ret;
}void SLInsertBF(SLNode* AFnode, SLDataType x)
{assert(AFnode);SLNode* newnode = BuySLNode(x);AFnode->prev->next = newnode;newnode->prev = AFnode->prev;AFnode->prev = newnode;newnode->next = AFnode;
}void SLInsertAF(SLNode* BFnode, SLDataType x)
{assert(BFnode);SLNode* newnode = BuySLNode(x);BFnode->next->prev = newnode;newnode->next = BFnode->next;BFnode->next = newnode;newnode->prev = BFnode;
}void SLDestroy(SLNode** phead)
{while ((*phead)->next != *phead)SLPopBack(*phead);free(*phead);
}

test.c

#include<stdio.h>
#include"List.h"void print(SLNode* head)
{SLNode* Head = head;head = head->next;printf("Head<->");while (Head != head){printf("%d<->", head->data);head = head->next;}printf("Head\n");
}void test1()
{SLNode* list1 = SLInit();SLPushBack(list1, 1);SLPushBack(list1, 2);SLPushBack(list1, 3);SLPushBack(list1, 4);SLPushFront(list1, 11);SLPushFront(list1, 22);print(list1);printf("Del = %d\n", SLPopBack(list1)); print(list1);printf("Del = %d\n", SLPopFront(list1)); print(list1);SLDestroy(&list1);
}int main()
{test1();return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 大规模装箱问题:蜣螂优化算法DBO求解二维装箱问题(MATLAB代码)
  • bash和sh区别
  • 牛客网刷题 | BC118 N个数之和
  • vue学习(一)
  • 代理模式(设计模式)
  • 【机器学习】集成学习方法:Bagging与Boosting的应用与优势
  • 安全测试框架
  • 怎么修改Visual Studio Code中现在github账号
  • HCIA 16 构建 IPv6 网络基础配置
  • Llama模型家族之拒绝抽样(Rejection Sampling)(九) 强化学习之Rejection Sampling
  • YOLOv8可视化界面PYQT5
  • 【C++进阶】模板进阶与仿函数:C++编程中的泛型与函数式编程思想
  • HTTP文件下载
  • Polkadot <> Kusama 桥:打造无信任互操作性的开创性范例
  • 从零开始开发知识付费APP:在线教育系统源码详解
  • Android优雅地处理按钮重复点击
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • Django 博客开发教程 8 - 博客文章详情页
  • eclipse的离线汉化
  • Git的一些常用操作
  • HTTP中的ETag在移动客户端的应用
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Java,console输出实时的转向GUI textbox
  • JavaScript创建对象的四种方式
  • Vue2.x学习三:事件处理生命周期钩子
  • Webpack 4 学习01(基础配置)
  • 阿里研究院入选中国企业智库系统影响力榜
  • 基于HAProxy的高性能缓存服务器nuster
  • 离散点最小(凸)包围边界查找
  • 实习面试笔记
  • 通过git安装npm私有模块
  • 微服务入门【系列视频课程】
  • 正则学习笔记
  • 阿里云API、SDK和CLI应用实践方案
  • 交换综合实验一
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • #Datawhale X 李宏毅苹果书 AI夏令营#3.13.2局部极小值与鞍点批量和动量
  • #define,static,const,三种常量的区别
  • #QT 笔记一
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (阿里云万网)-域名注册购买实名流程
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (二十三)Flask之高频面试点
  • (附源码)spring boot智能服药提醒app 毕业设计 102151
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (转)创业家杂志:UCWEB天使第一步
  • (转)负载均衡,回话保持,cookie
  • (转)平衡树
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .md即markdown文件的基本常用编写语法
  • .net core Redis 使用有序集合实现延迟队列
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端