# 循环链表链式存储与实现

• C3_371369
了解作者
• 2.7KB
文件大小
• zip
文件格式
• 0
收藏次数
• VIP专享
资源类型
• 0
下载次数
• 2022-06-15 01:45
上传日期

03-循环链表实现.zip
• 03-循环链表实现
• dm03_CircleList.h
847B
• dm03_循环链表and约瑟夫问题求解_main.cpp
1.5KB
• dm03_CircleList.cpp
4.8KB

#include "dm03_CircleList.h" typedef struct _tag_CircleList { CircleListNode header; CircleListNode* slider; //游标 int length; //循环链表长度 }TCircleList; CircleList* CircleList_Create() { TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList)); if (ret == nullptr) { return nullptr; } ret->length = 0; ret->header.next = nullptr; ret->slider = nullptr; return ret; } CircleList CircleList_Destroy(CircleList* list) { if (list == nullptr) { return; } free(list); //只free掉list？里面保存的元素呢？ list = nullptr; } //让链表恢复到初始化状态 CircleList CircleList_Clear(CircleList* list) { TCircleList* sList = (TCircleList*)list; if (sList == nullptr) { return; } sList->length = 0; sList->header.next = nullptr; sList->slider = nullptr; } int CircleList_Length(CircleList* list) { TCircleList* sList = (TCircleList*)list; if (sList == nullptr) { return -1; } return sList->length; } int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) { TCircleList* sList = (TCircleList*)list; if (sList == nullptr || node == nullptr || pos <0) { return -1; } { CircleListNode* current = (CircleListNode*)list; //刚开始current指向头部,此时实际上取出的是header元素的地址; int i = 0; //若pos有值，则current不会变化 for (; (i < pos) && (current->next != nullptr); i++) { current = current->next; } node->next = current->next; current->next = node; //若第一次插入节点--------------------异常1 if (0 == sList->length) { //sList->header.next = node; sList->slider = node; } sList->length++; //若插在第一个元素---------------------异常2 //若头插法 current仍然指向头部，需要重新指定循环列表最后一个元素的指向 //（原因是，跳0步，没有跳走） if (current == (CircleListNode*)sList) { //获取最后一个元素 CircleListNode* last = CircleList_Get(sList, sList->length-1); last->next = current->next; } } return 0; } CircleListNode* CircleList_Get(CircleList* list, int pos) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if (sList == nullptr || pos <0) { return ret; } int i = 0; CircleListNode* current = (CircleListNode*)list; //若pos有值，则current不会变化 for (; i < pos; i++) { current = current->next; } ret = current->next; return ret; } CircleListNode* CircleList_Delete(CircleList* list, int pos) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if ((sList != nullptr) &&(pos >=0) &&(sList->length > 0)) { CircleListNode* current = (CircleListNode*)list; //刚开始current指向头部 CircleListNode* last = nullptr; int i = 0; //若pos有值，则current不会变化 for (; (i < pos); i++) { current = current->next; } //若删除第一个元素（头结点）--------------异常1 if (current == (CircleListNode*)sList) { //获取最后一个元素 last = CircleList_Get(sList, sList->length - 1); } ret = current->next; current->next = ret->next; sList->length--; //此时last有值，处理删除第一个元素时的情况--------------异常1 if (nullptr != last) { sList->header.next = ret->next; last->next = ret->next; } //若删除元素为游标所指的元素 if (sList->slider == ret) { //游标下移 sList->slider = ret->next; } //若删除元素后，链表长度为0--------------异常1 if (sList->length == 0) { sList->header.next = nullptr; sList->slider = nullptr; } } return ret; } //循环列表游标的操作 CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if (sList != nullptr) { CircleListNode* current = (CircleListNode*)list; //刚开始current指向头部 int i = 0; for (; (i < sList->length); i++) { if (current->next == node) { ret = current->next; break; } current = current->next; } //如果ret找到，则根据i删除 //若删除元素为游标所指的元素 if (nullptr != ret) { CircleList_Delete(sList, i); } } return ret; } CircleListNode* CircleList_ResetSlider(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if (sList != nullptr) { sList->slider = sList->header.next; ret = sList->slider; } return ret; } CircleListNode* CircleList_CurrentSlider(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if (sList != nullptr) { ret = sList->slider; } return ret; } CircleListNode* CircleList_Next(CircleList* list) { TCircleList* sList = (TCircleList*)list; CircleListNode* ret = nullptr; if (sList != nullptr && sList->slider != nullptr) { ret = sList->slider; sList->slider = ret->next; } return ret; }

相关推荐