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

数据结构:循环双链表的基本操作(不带头结点)C语言

废话不多说!直接上代码!

#include <stdio.h>
#include <stdlib.h>#define bool char
#define true 1
#define false 0typedef int ElemType;/*[1].定义数据结构*/
// <1>定义结点的数据结构 
typedef struct Node {ElemType data;struct Node *prior, *next;
}Node, *PNode; 
// <2>定义链表的数据结构
typedef struct CLinkList {PNode head;PNode tail;int length; 
} CLinkList; /*[2].初始化不带头结点的循环双链表*/
bool InitList(CLinkList *L) {L->head = NULL;L->tail = NULL;L->length = 0;
}/*[3].遍历*/
bool PrintList(CLinkList *L) {if (L->head == NULL && L->tail == NULL && L->length == 0) {printf("the list is null!\n");return 0;}PNode p = L->head;printf("HEADNODE-->");for (int i=1; i<=L->length; i++) {printf("%d-->", p->data);p=p->next;if (i == L->length) {printf("HEADNODE\n"); }}return 1;
}/*[5].创建新结点*/
Node *CreateNewNode(ElemType e) {Node *new_node = (Node *)malloc(sizeof(Node));if (new_node == NULL) {return 0;} 	new_node->data = e;new_node->next = NULL;new_node->prior = NULL;return new_node;
} /*[4].前插法建立链表*/
bool HeadInsertList(CLinkList *L) {ElemType new_node_data;printf("please input the elem(end 9999): "); scanf("%d", &new_node_data);while(new_node_data != 9999) {PNode new_node = CreateNewNode(new_node_data);if (new_node == NULL) {return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {L->head = new_node;L->tail = new_node;L->length++;} else {new_node->next = L->head;L->head->prior = new_node;L->tail->next = new_node;new_node->prior = L->tail;L->head = new_node;L->length++;}printf("please input the elem(end 9999): "); scanf("%d", &new_node_data); }return 1;
} /*[5].尾插法建立链表*/
bool TailInsertList(CLinkList *L) {ElemType new_node_data;printf("please input the elem(end 9999): "); scanf("%d", &new_node_data);while(new_node_data != 9999) {PNode new_node = CreateNewNode(new_node_data);if (new_node == NULL) {return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {L->head = new_node;L->tail = new_node;L->length++;} else {PNode p = L->tail;new_node->next = p->next;p->next->prior = new_node;p->next = new_node;new_node->prior = p;L->tail=new_node;L->length++;}printf("please input the elem(end 9999): "); scanf("%d", &new_node_data); }return 1;
} /*[6].按位查找*/
PNode GetElem(CLinkList *L, int i) {if (i < 1 || i > L->length) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;}PNode p = L->head;int j=1;while(p != NULL && j < i) {p = p->next;j++;}return p;	
} /*[7].按值查找*/
PNode LocateElem(CLinkList *L, ElemType e) {if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;}PNode p = L->head;for (int i = 1; i <= L->length; i++) {p=p->next;if (p->data == e) {return p;}}
} /*[7].按位插入*/
bool InsertList(CLinkList *L, int i, ElemType e) {if (i < 1 || i > L->length+1) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode new_node = CreateNewNode(e); if (new_node == NULL) {return 0;}PNode p = L->head;int j = 1;while (p != NULL && j < i-1) {p = p->next;j++;}new_node->next = p->next;p->next->prior = new_node;p->next = new_node;new_node->prior = p;L->length++;return 1;
} /*[8].按位删除*/
bool DeleteList(CLinkList *L, int i) {if (i < 1 || i > L->length) {printf("the position of i is invalid!\n");return 0;}if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode p = L->head;int j = 1;while (p != NULL && j < i-1) {p = p->next;j++;}PNode q = p->next;  // p指向待删除的结点q->next->prior = p;p->next = q->next;free(q);return 1;
} /*[9].求链表长度*/
int LengthList(CLinkList *L) {int len = 1;PNode p = L->head;while (p->next != L->head) {p = p->next;len++;}return len;
} /*[10].销毁链表*/
bool DestoryList(CLinkList *L) {if (L->head==NULL && L->tail==NULL && L->length ==0) {printf("the list is null!\n");return 0;} PNode p = L->head;PNode q = NULL;int i=1;while (i <= L->length) {q = p->next;free(p);p = q;i++;}free(L->tail);L->head = NULL;L->tail = NULL;L->length = 0;return 1;
} int main() {CLinkList L;InitList(&L);PrintList(&L);HeadInsertList(&L);PrintList(&L);TailInsertList(&L);PrintList(&L);PNode p = GetElem(&L, 4);printf("按位查找到值为: %d\n", p->data);PNode p1 = LocateElem(&L, 23);printf("按值查找到结果为: %d\n", p1->data);InsertList(&L, 3, 666);PrintList(&L);DeleteList(&L, 5);PrintList(&L);int len = LengthList(&L);printf("表长为:%d\n", len);DestoryList(&L);PrintList(&L);return 0;
}

至此,线性表的相关代码更新完毕!喜欢的朋友们可以点赞关注,后续会继续更新栈、队列、树、图相关数据结构代码。

相关文章:

  • Spark与Hadoop之间有什么样的对比和联系
  • vant之 cell+picker+ popup 的踩坑
  • 优化提示词方面可以使用的数学方法理论:信息熵,概率论 ,最优化理论
  • MySQL 启动报错:InnoDB 表空间丢失问题及解决方法
  • C语言高频面试题——嵌入式系统中中断服务程序
  • 监控页面卡顿PerformanceObserver
  • 用Go语言正则,如何爬取数据
  • 豪越科技消防公车管理系统:智能化保障应急救援效率
  • 管理+技术”双轮驱动工业企业能源绿色转型
  • 第十一届机械工程、材料和自动化技术国际会议(MMEAT 2025)
  • linux基础14--dns和web+dns
  • vscode flutter 插件, vscode运行安卓项目,.gradle 路径配置
  • 动态规划算法:完全背包类问题
  • 739.每日温度
  • 鸿蒙Flutter仓库停止更新?
  • 加油站小程序实战教程13充值规则配置
  • Spark-SQL(总结)
  • 突破 RAG 检索瓶颈:Trae+MCP 构建高精度知识库检索系统实践
  • 1.微服务拆分与通信模式
  • EasyCVR视频智能分析平台助力智慧园区:全场景视频监控摄像头融合解决方案
  • 美国务院宣布新一轮与伊朗相关的制裁
  • 央行就《关于规范供应链金融业务引导供应链信息服务机构更好服务中小企业融资有关事宜的通知》答问
  • 马上评丨上热搜的协和“4+4”模式,如何面对舆论审视
  • 如何反击右翼思潮、弥合社会分裂:加拿大大选镜鉴
  • 助力企业高质量出海,上海静安发放服务包、服务券
  • 对话|贝聿铭设计的不只是建筑,更是生活空间