首页 > 其他分享 >实现-双向链表

实现-双向链表

时间:2023-08-28 16:58:11浏览次数:34  
标签:ptail 实现 double ptemp list 链表 双向 pdll linked

  1 /*
  2 简介:双向链表,可在头尾实现插入和删除
  3 注意:这个双向链表不形成环
  4 作者:njit-sam(许言)
  5 */
  6 
  7 #include <stdio.h>
  8 #include <stdlib.h>
  9 #include <string.h>
 10 
 11 typedef struct double_linked_list {
 12     int index;
 13     struct double_linked_list* pre;
 14     struct double_linked_list* next;
 15     void (*print)(void* p);
 16 } s_double_linked_list, *ps_double_linked_list;
 17 
 18 ps_double_linked_list phead = NULL;
 19 ps_double_linked_list ptail = NULL;
 20 
 21 void print(void* p) { printf("index=%d\n", *(int*)p); }
 22 
 23 void double_linked_list_init() {
 24     printf("init\n");
 25     phead = ptail = NULL;
 26 }
 27 
 28 void double_linked_list_insert_head() {
 29     if (phead == NULL) {
 30         ps_double_linked_list pdll = malloc(sizeof(s_double_linked_list));
 31         memset(pdll, '\0', sizeof(s_double_linked_list));
 32         pdll->index = 1;
 33         pdll->pre = pdll;
 34         pdll->next = NULL;
 35         pdll->print = print;
 36         phead = pdll;
 37         ptail = pdll;
 38     } else {
 39         ps_double_linked_list pdll = malloc(sizeof(s_double_linked_list));
 40         memset(pdll, '\0', sizeof(s_double_linked_list));
 41         pdll->index = 1;
 42         pdll->print = print;
 43         pdll->pre = pdll;
 44         pdll->next = phead;
 45         phead->pre = pdll;
 46         phead = pdll;
 47         /*重新整理index,使之从1开始有序*/
 48         int index = 1;
 49         ps_double_linked_list ptemp = phead->next;
 50         while (ptemp != NULL) {
 51             ptemp->index = ++index;
 52             ptail = ptemp;
 53             ptemp = ptemp->next;
 54         }
 55     }
 56 }
 57 
 58 void double_linked_list_insert_tail() {
 59     if (ptail == NULL) {
 60         ps_double_linked_list pdll = malloc(sizeof(s_double_linked_list));
 61         memset(pdll, '\0', sizeof(s_double_linked_list));
 62         pdll->index = 1;
 63         pdll->pre = pdll;
 64         pdll->next = NULL;
 65         pdll->print = print;
 66         phead = pdll;
 67         ptail = pdll;
 68     } else {
 69         ps_double_linked_list pdll = malloc(sizeof(s_double_linked_list));
 70         memset(pdll, '\0', sizeof(s_double_linked_list));
 71         pdll->index = ptail->index + 1;
 72         pdll->print = print;
 73         pdll->pre = ptail;
 74         pdll->next = NULL;
 75         ptail->next = pdll;
 76         ptail = pdll;
 77     }
 78 }
 79 
 80 void double_linked_list_remove_head() {
 81     if (phead == NULL)
 82         return;
 83     if (phead->next == NULL) {
 84         free(phead);
 85         phead = ptail = NULL;
 86     } else {
 87         ps_double_linked_list ptemp;
 88         ptemp = phead->next;
 89         ptemp->pre = ptemp;
 90         free(phead);
 91         phead = ptemp;
 92         /*重新整理index,使之从1开始有序*/
 93         int index = 0;
 94         ptemp = phead;
 95         while (ptemp != NULL) {
 96             ptemp->index = ++index;
 97             ptail = ptemp;
 98             ptemp = ptemp->next;
 99         }
100     }
101 }
102 
103 void double_linked_list_remove_tail() {
104     if (ptail == NULL)
105         return;
106     if (ptail->pre == ptail) {
107         free(ptail);
108         phead = ptail = NULL;
109     } else {
110         ps_double_linked_list ptemp;
111         ptemp = ptail->pre;
112         ptemp->next = NULL;
113         free(ptail);
114         ptail = ptemp;
115     }
116 }
117 
118 void double_linked_list_print() {
119     ps_double_linked_list ptemp = phead;
120     while (ptemp != NULL) {
121         ptemp->print(&ptemp->index);
122         ptemp = ptemp->next;
123     }
124 }
125 
126 int main(void) {
127     printf("Hello World\n");
128     double_linked_list_init();
129     double_linked_list_insert_head();
130     double_linked_list_insert_head();
131     double_linked_list_insert_head();
132     double_linked_list_insert_head();
133     double_linked_list_insert_head();
134     double_linked_list_insert_tail();
135     double_linked_list_insert_tail();
136     double_linked_list_insert_tail();
137     double_linked_list_insert_tail();
138     double_linked_list_print();
139     double_linked_list_remove_head();
140     double_linked_list_remove_head();
141     double_linked_list_print();
142     double_linked_list_remove_tail();
143     double_linked_list_remove_tail();
144     double_linked_list_print();
145     return 0;
146 }

 

标签:ptail,实现,double,ptemp,list,链表,双向,pdll,linked
From: https://www.cnblogs.com/njit-sam/p/17662774.html

相关文章

  • 阿里云 MSE 助力开迈斯实现业务高增长背后带来的服务挑战
    开迈斯新能源科技有限公司于2019年5月16日成立,目前合资股东分别为大众汽车(中国)投资有限公司、中国第一汽车股份有限公司、一汽-大众汽车有限公司[增资扩股将在取得适当监督(包括反垄断)审批后完成]、万帮数字能源股份有限公司和安徽江淮汽车集团控股有限公司,总部位于江苏常州......
  • 使用postMessage实现iframe和父页面通信
    语法语法otherWindow.postMessage(message,targetOrigin,[transfer]);otherWindow其他窗口的一个引用,例如向子窗口发送则otherWindow是子窗口的window对象父页面向子页面Iframe通信//父页面//获取iframe元素constiFrame:SafeAny=document.getElementById......
  • 如何实现字幕效果,cocos2dx ,Lua
    实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有C++,lua两个版本的代码:functionGameClientView:updateAdvertisement() --body localGG=self._Panel:getChildByName("notification_bg") localGGW=GG:getContentSize().width local......
  • 44基于java的汽车销售管理系统设计与实现(可参考做毕业设计)
    本章节给大家带来一个基于java的汽车销售管理系统设计与实现,车辆4S店管理系统,基于java汽车销售交易网站,针对汽车销售提供客户信息、车辆信息、订单信息、销售人员管理、财务报表等功能,提供经理和销售两种角色进行管理。引言实现一个汽车销售管理系统,汽车销售管理系统是一个大型......
  • C# wpf 实现窗口靠近屏幕边缘自动吸附
    参考:https://blog.csdn.net/qq_35831134/article/details/88751067#region侧吸privatenewboolHide=false;//用来表示当前隐藏状态,例如Hide=false就是不在隐藏状态stringtype="";//用来表示窗口隐藏在哪个方向pri......
  • strstr函数及其代码模拟实现
    一.用法定义:char*strstr(constchar*str1,constchar*str2);•判断str1中是否包含子串str2•若包含,则返回在str1中子串str2首字符的地址•若不包含,则返回空指针NULL例:#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<string.h>intmain(){ chararr1[]=......
  • 洗地机缺水检测如何通过管道液位传感器实现
    洗地机缺水检测可以通过管道液位传感器实现。传统的机械式传感器存在低精度和卡死失效的问题,而电容式传感器则存在感度衰减导致的不可控性失效。而光电管道传感器则有效解决了这些问题。光电管道传感器利用红外光学组件,通过设计形成感应线路。当水和空气中的光折率不同时,传感器能够......
  • 物通博联工业智能网关实现环保HJ212协议对接到水污染监控平台
    环保HJ212协议是一种用于环保数据上报的行业标准协议。它定义了数据格式、数据传输方式等规范,以便在全国范围内实现统一的环境监测和管理。HJ212协议上报的过程包括数据采集、数据封装、网络传输、服务器接收等步骤。 对此,物通博联推出的工业智能网关可以实现HJ212对接到水污染监......
  • Fast by BIP,企业“又快又省”实现财资管理数智化的“最强利器”
    8月26日,用友BIP全球司库事业部召开“FastbyBIP工作部署启动会”。针对用友BIP大型企业数智化速达包FastbyBIP中财资领域产品推广及落地作进一步详细规划。FastbyBIP是面向大型企业,全新推出的包含数智化基础设施、平台底座、应用软件、行业领先实践,以及技术迁移上云和一站式全......
  • 网页实现大文件上传下载
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......