博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
循环双链表
阅读量:5111 次
发布时间:2019-06-13

本文共 4301 字,大约阅读时间需要 14 分钟。

#include 
#include
typedef int ElementType;struct DoubleCircularListNode{ ElementType Element; struct DoubleCircularListNode *Prior; struct DoubleCircularListNode *Next;};struct DoubleCircularListNode* DoubleCircularListInit(){ struct DoubleCircularListNode* ListHead = malloc(sizeof(struct DoubleCircularListNode)); ListHead -> Prior = ListHead -> Next = ListHead; return ListHead;}int DoubleCircularListNodeAdd(struct DoubleCircularListNode *ListHead,ElementType ToBeInsert){ struct DoubleCircularListNode* TmpCell; struct DoubleCircularListNode* LastNode; TmpCell = malloc(sizeof(struct DoubleCircularListNode)); TmpCell -> Element = ToBeInsert; LastNode = ListHead -> Prior; TmpCell -> Prior = LastNode; TmpCell -> Next = ListHead; ListHead -> Prior = LastNode -> Next = TmpCell; return 0;}int DoubleCircularListIsEmpty(struct DoubleCircularListNode *ListHead){ return (ListHead->Next == ListHead);}int DoubleCircularListIsLast(struct DoubleCircularListNode *PtrToNode,struct DoubleCircularListNode *ListHead){ return (PtrToNode -> Next == ListHead);}//Warning:ensure the return pointer doesn't point to ListHeadstruct DoubleCircularListNode* DoubleCircularListNodeFind(ElementType ToBeFind,struct DoubleCircularListNode *ListHead){ struct DoubleCircularListNode* Pointer; Pointer = ListHead -> Next; while(Pointer != ListHead && Pointer -> Element != ToBeFind) { Pointer = Pointer -> Next; } return Pointer;}int DoubleCircularListNodeDelete(ElementType ToBeDelete,struct DoubleCircularListNode *ListHead){ struct DoubleCircularListNode *Pointer; Pointer = DoubleCircularListNodeFind(ToBeDelete,ListHead); if(Pointer == ListHead) { return 1; } else if(Pointer -> Prior == ListHead) { ListHead -> Prior = ListHead -> Next = ListHead; free(Pointer); return 0; } else { Pointer -> Next -> Prior = Pointer -> Prior; Pointer -> Prior -> Next = Pointer -> Next; free(Pointer); return 0; } return 1;}int MakeDoubleCircularListEmpty(struct DoubleCircularListNode *ListHead){ struct DoubleCircularListNode *Pointer_1; struct DoubleCircularListNode *Pointer_2; if(ListHead != NULL) { Pointer_1 = ListHead; Pointer_2 = ListHead -> Next; while(Pointer_2 != ListHead) { Pointer_1 = Pointer_2; Pointer_2 = Pointer_2 -> Next; free(Pointer_1); } ListHead -> Prior = ListHead -> Next = ListHead; } else { return 1; } return 0;}int DoubleCircularListDelete(struct DoubleCircularListNode *ListHead){ MakeDoubleCircularListEmpty(ListHead); free(ListHead); return 0;}int DoubleCircularListNodeInsert(struct DoubleCircularListNode *ListHead,ElementType ToBeInsert,struct DoubleCircularListNode *PositionBeforeToBeInsert){ struct DoubleCircularListNode* TmpCell; TmpCell = malloc(sizeof(struct DoubleCircularListNode)); TmpCell -> Element = ToBeInsert; if(TmpCell == NULL) { return 1; } TmpCell -> Prior = PositionBeforeToBeInsert; TmpCell -> Next = PositionBeforeToBeInsert -> Next; PositionBeforeToBeInsert -> Next -> Prior = TmpCell; PositionBeforeToBeInsert -> Next = TmpCell; return 0;}int main(){ struct DoubleCircularListNode* ListHead; ListHead = DoubleCircularListInit(); DoubleCircularListNodeInsert(ListHead,1,ListHead); DoubleCircularListNodeInsert(ListHead,3,DoubleCircularListNodeFind(1,ListHead)); DoubleCircularListNodeAdd(ListHead,4); DoubleCircularListNodeInsert(ListHead,2,DoubleCircularListNodeFind(1,ListHead)); int test = DoubleCircularListNodeDelete(2,ListHead); printf("test = %d %d\n",test,ListHead -> Next -> Element); printf("%d\n",ListHead -> Next -> Next -> Element); //printf("%d\n",ListHead -> Next -> Next -> Next -> Element); test = DoubleCircularListNodeDelete(2,ListHead); printf("test = %d %d\n",test,ListHead -> Next -> Next -> Element); //printf("%d\n",ListHead -> Next -> Next -> Next -> Element); //printf("%d\n",DoubleCircularListNodeFind(3,ListHead)->Element); DoubleCircularListDelete(ListHead); return 0;}

 

转载于:https://www.cnblogs.com/Asurudo/p/9427381.html

你可能感兴趣的文章
IO流的基础与小型项目
查看>>
机器学习之--KNN算法简单实现
查看>>
【转】NoSQL小故事:单服务器如何应付每秒75万次查询(2)
查看>>
[Unity Shader] 3D模型的简单属性
查看>>
壁纸推荐2018
查看>>
JQuery对象转dom ,dom转jQuery
查看>>
jquery 异步请求
查看>>
2018-09-25
查看>>
微信小程序开发---小程序对接Django---6
查看>>
[mysql] mysql批量操作时性能优化
查看>>
jQuery下拉菜单
查看>>
python习题:对比两个字典内容哪里不一样 并把不 一样的key和value打印出来
查看>>
不定长数组:vector
查看>>
pig grunt shell详解
查看>>
hadoop Shell命令详解
查看>>
jquery判断输入框的字符串是否为空或者空格
查看>>
NYOJ-44 简单DP
查看>>
java Integer与int详解 01
查看>>
OpenCV探索之路(十一):轮廓查找和多边形包围轮廓
查看>>
【Python】使用socketserver建立一个异步TCP服务器
查看>>