循环链表的实现
2014-07-13 01:46
85 查看
循环链表的实现
1>循环单链表的创建
2>打印输出循环单链表
3>释放循环单链表
4>在循环单链表中查找指定节点
5>在循环单链表的指定节点前插入节点
6>在循环单链表中删除指定节点
1>循环单链表的创建
2>打印输出循环单链表
3>释放循环单链表
4>在循环单链表中查找指定节点
5>在循环单链表的指定节点前插入节点
6>在循环单链表中删除指定节点
#include <iostream> typedef int DataType; typedef struct CirNode { DataType data; CirNode* next; }; typedef CirNode* CirLinkList; // 创建循环链表 CirLinkList CreateCirLinkList() { CirNode* head = (CirNode*) malloc(sizeof(CirNode)); if (NULL == head) { std::cout << "Malloc memory failed ~" << std::endl; exit(0); } CirNode* cur = head; std::cout << "Input the linklist elem and -1 end" << std::endl; DataType elem; while( std::cin >> elem) { if (-1 == elem) break; CirNode* node = (CirNode*) malloc(sizeof(CirNode)); if (NULL == node) { std::cout << "Malloc memory failed ~" << std::endl; exit(0); } node->data = elem; cur->next = node; cur = node; } cur->next = head; return head; } // 打印输出循环链表 void PrintCirLinkList(CirLinkList list) { if (NULL == list) return; CirNode* cur = list->next; std::cout << "CirLinkList elems ["; while(cur != list && cur != NULL) { std::cout << " " << cur->data; cur = cur->next; } std::cout << " ]" << std::endl; } // 释放循环链表 void DestoryCirLinkList(CirLinkList list) { if (NULL == list) return; CirNode* cur = list->next; CirNode* next; while(cur != list) { next = cur->next; free(cur); cur = next; } free(list); // 释放头结点 std::cout << "Destory cirlinklist successd " << std::endl; } // 查找循环链表 CirNode* FindCirNode(CirLinkList list,DataType val) { if (NULL == list) return NULL; CirNode* cur = list->next; while(cur != list) { if (cur->data == val) return cur; cur = cur->next; } return NULL; } // 在指定节点前插入新节点 成功返回1 int InsertCirNode(CirLinkList list, DataType val, DataType elem) { if (NULL == list) return -1; CirNode* cur = list; int flag = -1; while (cur->next != list) { if (cur->next->data == val) { flag = 1; break; } cur = cur->next; } if (-1 == flag) return -1; CirNode* node = (CirNode*) malloc(sizeof(CirNode)); if (NULL == node) return 0; node->data = elem; node->next = cur->next; cur->next = node; return 1; } // 删除指定节点 执行成功返回1 int DeleteCirNode(CirLinkList list, DataType elem) { if (NULL == list) return -1; CirNode* cur = list; int flag = -1; while (cur->next != list) { if (cur->next->data == elem) { flag = 1; break; } cur = cur->next; } if (-1 == flag) return -1; CirNode* tmp = cur->next; cur->next = tmp->next; free(tmp); return 1; } int main() { CirLinkList list = CreateCirLinkList(); PrintCirLinkList(list); DataType val, elem; std::cout << "Input Find val " << std::endl; std::cin >> val; CirNode* node = FindCirNode(list, val); if (NULL != node) { std::cout << "find node succeed " << node->data << std::endl; } else { std::cout << "Can't find node ~" << std::endl; } std::cout << "Input insert val and elem " << std::endl; std::cin >> val >> elem; if (int ans = InsertCirNode(list, val, elem) == 1) { std::cout << "Insert node succeed " << std::endl; PrintCirLinkList(list); }else { std::cout << "Insert node failed " << ans << std::endl; } std::cout << "Input delete elem " << std::endl; std::cin >> elem; if (1 == DeleteCirNode(list, elem)) { std::cout << "Delete node succeed " << std::endl; PrintCirLinkList(list); }else { std::cout << "delete node failed " << std::endl; } DestoryCirLinkList(list); return 0; }
相关文章推荐
- 约瑟夫环的循环链表实现
- C语言实现数据结构中的循环链表
- 链表的C语言实现之循环链表及双向链表
- 双向循环链表中结点类,链表类的定义与实现。
- 链表的C语言实现之循环链表及双向链表
- 单循环链表的实现
- 伪多态的循环链表C++实现【多类型支持】
- 数据结构实现循环链表
- 简单实现双向循环链表
- 双向循环链表在 LINUX kernel 中的实现
- 链表的C语言实现之循环链表及双向链表
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- JAVA实现循环链表
- 带头节点链表的反转 循环和递归 C#实现
- 一个简单的双向循环链表的实现
- 链表的C语言实现之循环链表及双向链表
- c#实现循环链表
- 双向循环链表删除算法的C++程序实现
- 双向循环链表的c++ 实现
- list.h双循环链表的实现,拷贝自Linux内核(2.6.20.1)