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

广安发展建设集团官方网站网站建设公司推来客网站系统怎么样

广安发展建设集团官方网站,网站建设公司推来客网站系统怎么样,永久免费云服务器无需注册,多个域名指向同一个网站双向链表的接口实现#xff08;附图解和源码#xff09; 文章目录双向链表的接口实现#xff08;附图解和源码#xff09;前言一、定义结构体二、接口实现#xff08;附图解源码#xff09;1.初始化双向链表2.开辟新空间3.尾插数据4.尾删数据5.打印双向链表中数据6.头插数…双向链表的接口实现附图解和源码 文章目录双向链表的接口实现附图解和源码前言一、定义结构体二、接口实现附图解源码1.初始化双向链表2.开辟新空间3.尾插数据4.尾删数据5.打印双向链表中数据6.头插数据7.头删数据8.查找结点位置9.在pos位置之前插入10.删除pos位置11.销毁双向链表三、源代码展示1.test.c测试主函数2.List.h接口函数的声明3.List.c接口函数的实现总结前言 本文主要介绍双向链表中增删查改等接口实现结尾附总源码 一、定义结构体 代码如下示例 typedef int LTDataType; typedef struct ListNode {LTDataType data;struct ListNode* prev;struct ListNode* next; }ListNode;二、接口实现附图解源码 这里一共10个接口我会我都会一 一为大家讲解图解源码 1.初始化双向链表 初始化出一个哨兵位的头结点用malloc开辟next和prev都指向自己。具体如下图 代码如下示例 ListNode* ListInit() {ListNode* p (ListNode*)malloc(sizeof(ListNode));if (p NULL){perror(errno);}ListNode* phead p;phead-next phead;phead-prev phead;return phead; }2.开辟新空间 (1)开辟一个链表类型的动态空间将地址给newnode (2)将值放入 newnode 的 data 数据内 (3)将 newnode 的 next 和 prev 都置为NULL 注意1.将malloc开辟空间存到 newnode 里面时参数为结构体所占的字节大小2.对 newnode 进行 NULL 判断 代码如下示例 ListNode* BuyListNode(LTDataType x) {ListNode* p (ListNode*)malloc(sizeof(ListNode));if (p NULL){perror(errno);}ListNode* newNode p;newNode-data x;newNode-next NULL;newNode-prev NULL;return newNode; }3.尾插数据 代码如下示例 void ListPushBack(ListNode* phead, LTDataType x)//尾插数据 {ListNode* tail phead-prev;ListNode* newnode BuyListNode(x);//第一个链接tail-next newnode;newnode-prev tail;//第二个链接phead-prev newnode;newnode-next phead; }4.尾删数据 这里需要两次进行断言 代码如下示例 void ListPopBack(ListNode* phead)//尾删 {assert(phead);assert(phead-next ! phead);ListNode* tail phead-prev;ListNode* tailprev tail-prev;free(tail);//连接tailprev-next phead;phead-prev tailprev; }5.打印双向链表中数据 代码如下示例 void ListPrint(ListNode* phead)//打印数据 {assert(phead);ListNode* cur phead-next;while (cur ! phead){printf(%d , cur-data);cur cur-next;}printf(\n); }6.头插数据 头插数据也是很简单的一个接口链接两次即可如下图 代码如下示例 void ListPushFront(ListNode* phead, LTDataType x)//头插 {assert(phead);ListNode* newNode BuyListNode(x);ListNode* next phead-next;phead-next newNode;newNode-prev phead;newNode-next next;next-prev newNode; }7.头删数据 在头删数据时和尾删数据一样都需要进行两次断言如下图 代码如下示例 void ListPopFront(ListNode* phead)//头删 {assert(phead);assert(phead-next ! phead);ListNode* next phead-next;ListNode* nextNext next-next;phead-next nextNext;nextNext-prev phead;free(next); }8.查找结点位置 这个比较简单和单链表一样直接用cur遍历即可直接上代码 代码如下示例 ListNode* ListFind(ListNode* phead, LTDataType x) {assert(phead);ListNode* cur phead-next;while (cur ! phead){if (cur-data x){return cur;}cur cur-next;}return NULL; }9.在pos位置之前插入 代码如下示例 void ListInsert(ListNode* pos, LTDataType x)//pos位置之前插入 {assert(pos);ListNode* newNode BuyListNode(x);ListNode* posPrev pos-prev;// posPrev newnode posposPrev-next newNode;newNode-prev posPrev;newNode-next pos;pos-prev newNode; }根据ListInsert接口我们可以把头插和尾插进行改版如下图 10.删除pos位置 代码如下示例 void ListErase(ListNode* pos)//删除pos位置 {assert(pos);ListNode* prev pos-prev;ListNode* next pos-next;free(pos);pos NULL;prev-next next;next-prev prev; }11.销毁双向链表 代码如下示例 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; }三、源代码展示 1.test.c测试主函数 代码如下示例 #include list.h void Testlist1() {ListNode* n1 ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插//ListPushBack(n1, 3);//尾插//ListPushBack(n1, 4);//尾插//ListPushBack(n1, 5);//尾插//ListPopFront(n1);//头删//ListPopBack(n1);//尾删//ListPushFront(n1, 0);//头插ListPrint(n1); } void Testlist2() {ListNode* n1 ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插ListPushBack(n1, 3);//尾插ListPushBack(n1, 4);//尾插ListPushBack(n1, 5);//尾插ListNode* p ListFind(n1, 5);printf(%d \n, p-data); } void Testlist3() {ListNode* n1 ListInit();ListPushBack(n1, 1);//尾插ListPushBack(n1, 2);//尾插ListPushBack(n1, 3);//尾插ListPushBack(n1, 4);//尾插ListPushBack(n1, 5);//尾插//ListNode* p ListFind(n1, 5);//ListInsert(p, 0);ListDestory(n1);ListPrint(n1); } int main() {//Testlist1();//Testlist2();Testlist3();return 0; }2.List.h接口函数的声明 代码如下示例 #include stdio.h #include stdlib.h #include assert.h #include errno.h typedef int LTDataType; typedef struct ListNode {LTDataType data;struct ListNode* prev;struct ListNode* next; }ListNode; ListNode* ListInit();//初始化双向链表 void ListPushBack(ListNode* phead, LTDataType x);//尾插 void ListPopBack(ListNode* phead);//尾删 void ListPushFront(ListNode* phead, LTDataType x);//头插 void ListPopFront(ListNode* phead);//头删 void ListPrint(ListNode* phead);//打印 ListNode* ListFind(ListNode* phead, LTDataType x);//查找数据 void ListInsert(ListNode* pos, LTDataType x);//pos位置之前插入 void ListErase(ListNode* pos);//删除pos位置 void ListDestory(ListNode* phead);//销毁双向链表3.List.c接口函数的实现 代码如下示例 #include list.h ListNode* ListInit() {ListNode* p (ListNode*)malloc(sizeof(ListNode));if (p NULL){perror(errno);}ListNode* phead p;phead-next phead;phead-prev phead;return phead; } ListNode* BuyListNode(LTDataType x) {ListNode* p (ListNode*)malloc(sizeof(ListNode));if (p NULL){perror(errno);}ListNode* newNode p;newNode-data x;newNode-next NULL;newNode-prev NULL;return newNode; } void ListPushBack(ListNode* phead, LTDataType x)//尾插数据 {ListNode* tail phead-prev;ListNode* newnode BuyListNode(x);//第一个链接tail-next newnode;newnode-prev tail;//第二个链接phead-prev newnode;newnode-next phead;//ListInsert(phead, x); } void ListPrint(ListNode* phead)//打印数据 {assert(phead);ListNode* cur phead-next;while (cur ! phead){printf(%d , cur-data);cur cur-next;}printf(\n); } void ListPopBack(ListNode* phead)//尾删 {assert(phead);assert(phead-next ! phead);ListNode* tail phead-prev;ListNode* tailprev tail-prev;free(tail);//连接tailprev-next phead;phead-prev tailprev; } void ListPushFront(ListNode* phead, LTDataType x)//头插 {assert(phead);ListNode* newNode BuyListNode(x);ListNode* next phead-next;phead-next newNode;newNode-prev phead;newNode-next next;next-prev newNode;//ListInsert(phead-next, x); } void ListPopFront(ListNode* phead)//头删 {assert(phead);assert(phead-next ! phead);ListNode* next phead-next;ListNode* nextNext next-next;phead-next nextNext;nextNext-prev phead;free(next); } ListNode* ListFind(ListNode* phead, LTDataType x) {assert(phead);ListNode* cur phead-next;while (cur ! phead){if (cur-data x){return cur;}cur cur-next;}return NULL; } void ListInsert(ListNode* pos, LTDataType x)//pos位置之前插入 {assert(pos);ListNode* newNode BuyListNode(x);ListNode* posPrev pos-prev;// posPrev newnode posposPrev-next newNode;newNode-prev posPrev;newNode-next pos;pos-prev newNode; } void ListErase(ListNode* pos)//删除pos位置 {assert(pos);ListNode* prev pos-prev;ListNode* next pos-next;free(pos);pos NULL;prev-next next;next-prev prev; } 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; }总结 以上就是今天要讲的内容本文介绍双向链表的接口实现附图解和源码。 如果我的博客对你有所帮助记得三连支持一下感谢大家的支持
http://www.hkea.cn/news/14475176/

相关文章:

  • 濮阳公司做网站wordpress 添加rss
  • 单页面网站有哪些内容吗wordpress自动水印代码
  • 购物帮–做特惠的导购网站绍兴专业做网站
  • 前几年做啥网站致富网站后台管理无法编辑
  • 跨平台网站制作网络优化怎么弄
  • 手机端的网站怎么做域名申请后怎么建网站
  • 邵阳高端网站建设中国建设银行门户网站企业
  • 网站经常被挂马中铁建设集团官方网站
  • 芙蓉建设官方网站网站建设保障措施
  • wordpress lampp建站白云移动网站建设
  • 网站架构基本知识中山专业手机网站建设
  • 个人免费网站制作上海招投标网官方
  • 江苏专业网站推广公司百度网站开发业务
  • 成都知名网站建设软件免费网站大全
  • 网站设计公司域名服务器建设快速网站排名优化
  • 通过骗子网站能找到其服务器吗贵阳网站建设功能
  • 网站建设和维护工作内容网络营销推广8种方法
  • 上海网站建设方案托管单页营销分享网站
  • 怎样让百度搜索到自己的网站帝国cms做漫画网站教程
  • 连云港网站关键词优化可信网站身份验证 必须做吗
  • 济源网站优化黄埔移动网站建设
  • 网站开发具体工作有那些网站设计职业工作室
  • 下载wix做的网站php软件网站建设
  • 山东城乡建设厅网站首页抖音 运营
  • 网站制作模板过程北京各大网站推广平台哪家好
  • 西安演出公司网站建设福田做网站福田网站建设福田建网站500
  • 网站如何快速收录广州网站设计哪家公司好
  • 河南秋实网站建设手机app开发定制公司
  • 长春制作网站哪家好lovevideo门户视频wordpress主题
  • 哈尔滨服务专业的建站wordpress百度统计代码