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

西部数码网站管理助手搭建织梦无锡网站排名优化公司

西部数码网站管理助手搭建织梦,无锡网站排名优化公司,如何快速的做网站,手机网站 搜索优化 百度目录 前言#xff1a; 一#xff1a;双链表的定义 ​编辑 二#xff1a;双向链表的实现 2.1#xff1a;链表的构造 2.2#xff1a;创建头节点 2.3#xff1a;创建节点 2.4#xff1a;链表的尾插 2.5#xff1a;链表的打印 2.6#xff1a;链表的尾删 2.7 一双链表的定义 ​编辑 二双向链表的实现 2.1链表的构造 2.2创建头节点 2.3创建节点  2.4链表的尾插  2.5链表的打印 2.6链表的尾删 2.7链表的头插 2.8链表的头删 2.9链表的查找  2.10在目标位置前面插入 2.11删除目标位置结点 2.12链表的销毁 总代码 test.c List.c List.h 前言 双链表的引入是因为单链表要访问某个结点的前驱结点时只能从头开始遍历访问后驱结点的复杂度为O(1)访问前驱结点的复杂度为O(n)。为了克服上述缺点引入了双链表。 双链表的引进对于链表的操作有了极大的遍历 一双链表的定义 链表由单向的链变成了双向链。 双向链表(double linked list)是在单链表的每个结点中再设置一个指向其前驱结点的指针域。  二双向链表的实现 2.1链表的构造 包含了一个数据域两个指针域指向前后驱节点 // 带头双向循环链表增删查改实现 typedef int LTDataType; typedef struct ListNode {LTDataType data; //数据struct ListNode* next; //下一个指针域struct ListNode* prev; //上一个指针域 }ListNode; 2.2创建头节点 双向链表一般都是带头节点的在链表中带了头节点对于链表分割这一问题有了简单化 动态开辟出一块空间前后指针都指向自己 //初始化链表头头节点 ListNode* ListInit() {ListNode* phead (ListNode*)malloc(sizeof(ListNode));assert(phead);phead-data -1;phead-next phead;phead-prev phead;return phead; } 2.3创建节点  这里置NULL和单链表的置NULL是一样的意思对后续的操作提供便利 // 创建返回链表的结点. ListNode* ListCreate(LTDataType x) {ListNode* newnode (ListNode*)malloc(sizeof(ListNode));assert(newnode);newnode-data x;newnode-next NULL;newnode-prev NULL;return newnode; } 2.4链表的尾插  单链表的尾插还需要考虑是否存在第一个节点这里直接插入即可 注意操作顺序 // 双向链表尾插 void ListPushBack(ListNode* phead, LTDataType x) {assert(phead);ListNode* newnode ListCreate(x);struct ListNode* tail phead-prev;// phead tail newnode//newnode-prev phead-prev;//newnode-next phead;//phead-prev-next newnode;//phead-prev newnode;//注意前后顺序newnode-prev tail;tail-next newnode;newnode-next phead;phead-prev newnode;} 2.5链表的打印 这里的关键就是从哪里开始如何判断结束因为是循环 我们可以从头结点的下一个开始打印当遇到头结点即是结束 // 双向链表打印 void ListPrint(ListNode* phead) {assert(phead);struct ListNode* cur phead-next;printf(哨兵位);while (cur ! phead){printf(%d, cur-data);cur cur-next;}printf(\n); } 2.6链表的尾删 要多一个断言判断如果只有一个头指针就不用操作了 保存尾结点的前驱释放尾结点即可 // 双向链表尾删 void ListPopBack(ListNode* phead) {assert(phead);assert(phead-next ! phead); //只有头指针不删struct ListNode* cur phead-prev;struct ListNode* curPrev cur-prev; //尾节点的上一个phead-prev curPrev;curPrev-next phead;free(cur);cur NULL; } 2.7链表的头插 和尾插操作相似定义头节点的下一个结点进行链接即可 注意顺序 // 双向链表头插 void ListPushFront(ListNode* phead, LTDataType x) {assert(phead);ListNode* newnode ListCreate(x);struct ListNode* cur phead-next;newnode-next cur; cur-prev newnode;newnode-prev phead;phead-next newnode; } 2.8链表的头删 删除操作一般都需要判断一下是否只有头节点。判断双向链表的条件是phead-next ! phead; // 双向链表头删 void ListPopFront(ListNode* phead) {assert(phead);assert(phead-next ! phead); //只有头指针不删ListNode* cur phead-next;ListNode* next cur-next;phead-next next;next-prev phead;free(cur);cur NULL; } 2.9链表的查找  找到该结点后返回的是指针而不是数据返回该指针位置方便后续操作 // 双向链表查找 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; } 2.10在目标位置前面插入 // 双向链表在pos的前面进行插入 void ListInsert(ListNode* pos, LTDataType x) {assert(pos); //检查pos位置是否有效ListNode* newnode ListCreate(x);newnode-next pos; //将newnode节点next prev 链接前后节点newnode-prev pos-prev;pos-prev-next newnode;pos-prev newnode; } 2.11删除目标位置结点 // 双向链表删除pos位置的节点 void ListErase(ListNode* pos) {assert(pos);ListNode* posPrev pos-prev;ListNode* next pos-next;posPrev-next next;next-prev posPrev;free(pos);pos NULL; } 2.12链表的销毁 // 双向链表销毁 void ListDestory(ListNode* phead) {assert(phead);ListNode* cur phead-prev;while (cur ! phead) //将除了头结点的都销毁{ListNode* curPrev cur-prev;free(cur);cur curPrev;}free(phead); //再释放头结点//phead NULL; } 总代码 test.c #define _CRT_SECURE_NO_WARNINGS 1 #includeList.hvoid test1() {ListNode* plist NULL;plist ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);ListPopBack(plist);ListPrint(plist);}void test2() {ListNode* plist NULL;plist ListInit();ListPrint(plist);//ͷListPushFront(plist, 6);ListPrint(plist);//ͷɾListPopFront(plist);ListPrint(plist);}void test3() {ListNode* plist NULL;plist ListInit();ListPushBack(plist, 1);ListPushBack(plist, 2);ListPushBack(plist, 3);ListPushBack(plist, 4);ListPrint(plist);//βɾListPopBack(plist);ListPopBack(plist);ListPopBack(plist);ListNode* pos ListFind(plist,1);ListInsert(pos, 666);ListPrint(plist);ListErase(pos);ListPrint(plist);ListDestory(plist); } int main() {//test1();//test2();test3();return 0; } List.c #define _CRT_SECURE_NO_WARNINGS 1 #includeList.h//初始化链表头头节点 ListNode* ListInit() {ListNode* phead (ListNode*)malloc(sizeof(ListNode));assert(phead);phead-data -1;phead-next phead;phead-prev phead;return phead; }// 创建返回链表的头结点. ListNode* ListCreate(LTDataType x) {ListNode* newnode (ListNode*)malloc(sizeof(ListNode));assert(newnode);newnode-data x;newnode-next NULL;newnode-prev NULL;return newnode; }// 双向链表尾插 void ListPushBack(ListNode* phead, LTDataType x) {assert(phead);ListNode* newnode ListCreate(x);struct ListNode* tail phead-prev;// phead tail newnode//newnode-prev phead-prev;//newnode-next phead;//phead-prev-next newnode;//phead-prev newnode;//注意前后顺序newnode-prev tail;tail-next newnode;newnode-next phead;phead-prev newnode;}// 双向链表打印 void ListPrint(ListNode* phead) {assert(phead);struct ListNode* cur phead-next;printf(哨兵位);while (cur ! phead){printf(%d, cur-data);cur cur-next;}printf(\n); }// 双向链表尾删 void ListPopBack(ListNode* phead) {assert(phead);assert(phead-next ! phead); //只有头指针不删struct ListNode* cur phead-prev;struct ListNode* curPrev cur-prev; //尾节点的上一个phead-prev curPrev;curPrev-next phead;free(cur);cur NULL; }// 双向链表头插 void ListPushFront(ListNode* phead, LTDataType x) {assert(phead);ListNode* newnode ListCreate(x);struct ListNode* cur phead-next;newnode-next cur; cur-prev newnode;newnode-prev phead;phead-next newnode; }// 双向链表头删 void ListPopFront(ListNode* phead) {assert(phead);assert(phead-next ! phead); //只有头指针不删ListNode* cur phead-next;ListNode* next cur-next;phead-next next;next-prev phead;free(cur);cur NULL; }// 双向链表查找 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; }// 双向链表在pos的前面进行插入 void ListInsert(ListNode* pos, LTDataType x) {assert(pos); //检查pos位置是否有效ListNode* newnode ListCreate(x);newnode-next pos; //将newnode节点next prev 链接前后节点newnode-prev pos-prev;pos-prev-next newnode;pos-prev newnode; }// 双向链表删除pos位置的节点 void ListErase(ListNode* pos) {assert(pos);ListNode* posPrev pos-prev;ListNode* next pos-next;posPrev-next next;next-prev posPrev;free(pos);pos NULL; }// 双向链表销毁 void ListDestory(ListNode* phead) {assert(phead);ListNode* cur phead-prev;while (cur ! phead) //将除了头节点的都销毁{ListNode* curPrev cur-prev;free(cur);cur curPrev;}free(phead);//phead NULL; } List.h #pragma once #includestdio.h #includeassert.h #includestdlib.h// 带头双向循环链表增删查改实现 typedef int LTDataType; typedef struct ListNode {LTDataType data; //数据struct ListNode* next; //下一个指针域struct ListNode* prev; //上一个指针域 }ListNode;// 创建返回链表的头结点. ListNode* ListCreate(LTDataType x);//初始化链表头头节点 ListNode* ListInit();// 双向链表尾插 void ListPushBack(ListNode* pHead, LTDataType x);// 双向链表打印 void ListPrint(ListNode* pHead);// 双向链表尾删 void ListPopBack(ListNode* pHead);// 双向链表头插 void ListPushFront(ListNode* pHead, LTDataType x);// 双向链表头删 void ListPopFront(ListNode* pHead);// 双向链表查找 ListNode* ListFind(ListNode* pHead, LTDataType x);// 双向链表在pos的前面进行插入 void ListInsert(ListNode* pos, LTDataType x);// 双向链表删除pos位置的节点 void ListErase(ListNode* pos);// 双向链表销毁 void ListDestory(ListNode* pHead); 以上就是我对【数据结构|双向链表|增删改查】的介绍不足之处还望指点。
http://www.yutouwan.com/news/159077/

相关文章:

  • 百度财报q3优化营商环境条例
  • 网站定制公司平顶山高端网站建设
  • iis做网站视手机网站域名哪里注册时间
  • 流量套餐汇总网站外贸做的社交网站
  • 电子商务网站开发策划可以讨论网站建设的论坛
  • 网站开发语言排名wordpress自动添加
  • 公司域名让做网站的网站开发行业分析
  • 做微信扫码网站牡丹江建设厅网站
  • 给网站做绝对路径怎么给公司做网站
  • 沧浪企业建设网站价格淘客招商网站选品库建设
  • 静态网站建设的技术运用建设局网站功能简介
  • 免费企业网站建设哪家搜狗网站做滤芯怎么样
  • 建筑公司是干什么的seo的定义
  • 网站建设银行业务预约纪念币猪年纪念币预约江门网站优化方案
  • 外贸英文网站开发长春做网站哪里好
  • 做网站的图片要求大小虚拟电子商务网站建设前期规划方案
  • 做家装的网站有哪些百度一下就知道首页
  • 上海外贸网站建设找哪家衡水专业网站建设公司
  • 龙岗爱联网站建设网站建设的工作职责是什么
  • 娄底工程建设有限公司网站成品免费观看网站
  • 正规网站建设首选公司电脑ppt制作软件
  • 门户网站的意思网站建设完成
  • 网站打开空白页不用购买域名做网站
  • 给别人做网站的话术南京seo排名优化
  • 电子商务网站建设实训报告网址注册了怎么做网站
  • 网站开发项目实训总结建设工程安全管理中心网站
  • 平台网站建设公司中职网站建设与维护试卷
  • 织梦做英文网站出现乱码莱芜新闻民生广角
  • 湖南网站建设网络公司推荐网站制作公司
  • 网站投票系统 js网上购物系统的设计与实现论文