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

循环链表 -- c语言实现

#pragma once
// 带头+双向+循环链表增删查改实现
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>typedef int LTDataType;typedef struct ListNode
{LTDataType data;struct ListNode* next;struct ListNode* prev;
}ListNode;//双链表申请一个新节点
ListNode* BuyListNode(LTDataType x);// 创建返回链表的头结点
ListNode* ListCreate();// 双向链表打印
void ListPrint(ListNode* pHead);// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);// 双向链表尾删
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);//双链表申请一个新节点
ListNode* BuyListNode(LTDataType x)
{ListNode* node = (ListNode*)malloc(sizeof(ListNode));if (node == NULL){perror("BuyListNode::malloc");return NULL;}node->data = x;node->next = NULL;node->prev = NULL;return node;
}// 创建返回链表的头结点
ListNode* ListCreate()
{ListNode* head = BuyListNode(-1);head->next = head;head->prev = head;
}// 双向链表打印
void ListPrint(ListNode* pHead)
{assert(pHead);ListNode* cur = pHead->next;printf("head<=>");while (cur != pHead){printf("%d<=>", cur->data);cur = cur->next;}printf("\n");return;
}// 双向链表尾插
// void ListPushBack(ListNode* pHead, LTDataType x)
// {
// 	assert(pHead);
// 	ListNode* newnode = BuyListNode(x);
// 	ListNode* tail = pHead->prev;
// 	tail->next = newnode;
// 	newnode->prev = tail;
// 	newnode->next = pHead;
// 	pHead->prev = newnode;
// 	return;
// }
// 下面的写法更容易理解
void ListPushBack(ListNode* pHead, LTDataType x)
{assert(pHead);ListNode* newnode = BuyListNode(x);newnode->next = pHead;newnode->prev = pHead->prev;pHead->prev->next = newnode;pHead->prev = newnode;return;}// 双向链表尾删
void ListPopBack(ListNode* pHead)
{assert(pHead);ListNode* tail = pHead->prev;if (tail == pHead)return;tail->prev->next = pHead;pHead->prev = tail->prev;free(tail);tail = NULL;return;
}// 双向链表头插
// void ListPushFront(ListNode* pHead, LTDataType x)
// {
// 	assert(pHead);
// 	ListNode* newnode = BuyListNode(x);
// 	newnode->next = pHead->next;
// 	pHead->next->prev = newnode;
// 	pHead->next = newnode;
// 	newnode->prev = pHead;
// 	return;
// }void ListPushFront(ListNode* pHead, LTDataType x)
{assert(pHead);ListNode* newnode = BuyListNode(x);newnode->next = pHead->next;newnode->prev = pHead;pHead->next->prev = newnode;pHead->next = newnode;return;
}// 双向链表头删
void ListPopFront(ListNode* pHead)
{assert(pHead);ListNode* tmp = pHead->next;if (tmp == pHead)return;tmp->next->prev = pHead;pHead->next = tmp->next;free(tmp);tmp = NULL;
}// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{assert(pHead);ListNode* cur = pHead;while (cur->data != x){cur = cur->next;if (cur == pHead)return NULL;}return cur;
}// 双向链表在pos的前面进行插入
// void ListInsert(ListNode* pos, LTDataType x)
// {
// 	assert(pos);
// 	ListNode* newnode = BuyListNode(x);
// 	pos->prev->next = newnode;
// 	newnode->prev = pos->prev;
// 	pos->prev = newnode;
// 	newnode->next = pos;
// 	return;
// }
// 下面这个更好理解,且避免了pos为头节点时若头节点的 next 为 NULL 时的错误
void ListInsert(ListNode* pos, LTDataType x)
{assert(pos);ListNode* newnode = BuyListNode(x);newnode->next = pos;newnode->prev = pos->prev;pos->prev->next = newnode;pos->prev = newnode;return;
}// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{assert(pos);pos->prev->next = pos->next;pos->next->prev = pos->prev;free(pos);pos = NULL;return;
}// 双向链表销毁
void ListDestory(ListNode* pHead)
{assert(pHead);ListNode* cur = pHead->next;while (cur != pHead){ListNode* tmp = cur->next;free(cur);cur = tmp;}free(pHead);return;
}int main() {// 创建一个双向循环链表,头节点headListNode* myList = ListCreate();// 尾插元素ListPushBack(myList, 1);ListPushBack(myList, 2);ListPushBack(myList, 3);// 头插元素ListPushFront(myList, 0);// 打印链表ListPrint(myList); // 应该输出:head<=>0<=>1<=>2<=>3<=>// 查找元素ListNode* foundNode = ListFind(myList, 2);if (foundNode != NULL) {printf("找到的节点的data: %d\n", foundNode->data); // 应该输出:Found node with value: 2} else {printf("没有找到\n");}// 在指定位置前插入元素ListInsert(foundNode, 10);// 打印链表ListPrint(myList); // 应该输出:head<=>0<=>1<=>10<=>2<=>3<=>// 删除指定位置的元素ListErase(foundNode);// 打印链表ListPrint(myList); // 应该输出:head<=>0<=>1<=>10<=>3<=>// 头删元素ListPopFront(myList);// 尾删元素ListPopBack(myList);// 打印链表ListPrint(myList); // 应该输出:head<=>1<=>2<=>// 打印 head 的 prev 的 dataprintf("head->prev->data: %d", myList->prev->data);// 销毁链表ListDestory(myList);return 0;
}

运行结果:

http://www.lryc.cn/news/342659.html

相关文章:

  • 如何使git提交的时候忽略一些特殊文件?
  • 如何保证Redis双写一致性?
  • HarmonyOS实战开发-如何实现查询当前城市实时天气功能
  • (三)JSP教程——JSP动作标签
  • centos7安装真的Redmine-5.1.2+ruby-3.0.0
  • 方法的重写
  • Terraform局部值
  • vue+element-ui实现横向长箭头,横向线上下可自定义文字(使用after伪元素实现箭头)
  • 性能监控之prometheus+grafana搭建
  • 25-ESP32-S3 内置的真随机数发生器(RNG)
  • 万兆以太网MAC设计(12)万兆UDP协议栈上板与主机网卡通信
  • 2024年4月17日华为春招实习试题【三题】-题目+题解+在线评测,2024.4.17,华为机试
  • 展开说说:Android线程池解析
  • Selenium自动化测试面试题全家桶
  • Docker 容器日志占用空间过大解决办法
  • update_min_vruntime()流程图
  • 十进制转任意进制(以及任意进制来回转换<了解>)
  • postcss-px-to-viewport 从入坑到放弃 (nuxt3搭建响应式官网解决方案 )
  • C语言从入门到入门
  • Java基础教程 - 4 流程控制
  • 大厂Java面试题:MyBatis中有几种加载映射器(Mapper.xml)的方式?
  • Flutter笔记:Widgets Easier组件库(10)快速处理承若型对话
  • 10_Linux中的计划任务
  • Google Play开发者账号为什么会被封?如何解决关联账号问题?
  • (第12天)【leetcode题解】151、反转字符串中的单词
  • 如何处理多模态数据噪声不均衡动态?天大等最新《低质量数据的多模态融合》综述
  • Autosar NvM配置-手动配置Nvblock及使用-基于ETAS软件
  • 【c++算法篇】双指针(下)
  • 微图乐 多种装B截图一键制作工具(仅供娱乐交流)
  • 基于Springboot的点餐平台