循环链表
Posted xiaowulang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环链表相关的知识,希望对你有一定的参考价值。
1.循环链表只是在单链表的基础上做了一个加强
2.循环链表可以完全取代单链表的使用
3.循环链表的Next和Current操作可以高效的遍历链表中的所有元素
1 #include <stdio.h> 2 #include <malloc.h> 3 #include "CircleList.h" 4 5 typedef struct _tag_CircleList 6 { 7 CircleListNode header; 8 CircleListNode* slider; 9 int length; 10 } TCircleList; 11 12 CircleList* CircleList_Create() // O(1) 13 { 14 TCircleList* ret = (TCircleList*)malloc(sizeof(TCircleList)); 15 16 if( ret != NULL ) 17 { 18 ret->length = 0; 19 ret->header.next = NULL; 20 ret->slider = NULL; 21 } 22 23 return ret; 24 } 25 26 void CircleList_Destroy(CircleList* list) // O(1) 27 { 28 free(list); 29 } 30 31 void CircleList_Clear(CircleList* list) // O(1) 32 { 33 TCircleList* sList = (TCircleList*)list; 34 35 if( sList != NULL ) 36 { 37 sList->length = 0; 38 sList->header.next = NULL; 39 sList->slider = NULL; 40 } 41 } 42 43 int CircleList_Length(CircleList* list) // O(1) 44 { 45 TCircleList* sList = (TCircleList*)list; 46 int ret = -1; 47 48 if( sList != NULL ) 49 { 50 ret = sList->length; 51 } 52 53 return ret; 54 } 55 56 int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) // O(n) 57 { 58 TCircleList* sList = (TCircleList*)list; 59 int ret = (sList != NULL) && (pos >= 0) && (node != NULL); 60 int i = 0; 61 62 if( ret ) 63 { 64 CircleListNode* current = (CircleListNode*)sList; 65 66 for(i=0; (i<pos) && (current->next != NULL); i++) 67 { 68 current = current->next; 69 } 70 71 node->next = current->next; 72 current->next = node; 73 74 if( sList->length == 0 ) 75 { 76 sList->slider = node; 77 node->next = node; 78 } 79 80 sList->length++; 81 } 82 83 return ret; 84 } 85 86 CircleListNode* CircleList_Get(CircleList* list, int pos) // O(n) 87 { 88 TCircleList* sList = (TCircleList*)list; 89 CircleListNode* ret = NULL; 90 int i = 0; 91 92 if( (sList != NULL) && (pos >= 0) ) 93 { 94 CircleListNode* current = (CircleListNode*)sList; 95 96 for(i=0; i<pos; i++) 97 { 98 current = current->next; 99 } 100 101 ret = current->next; 102 } 103 104 return ret; 105 } 106 107 CircleListNode* CircleList_Delete(CircleList* list, int pos) // O(n) 108 { 109 TCircleList* sList = (TCircleList*)list; 110 CircleListNode* ret = NULL; 111 int i = 0; 112 113 if( (sList != NULL) && (pos >= 0) ) 114 { 115 CircleListNode* current = (CircleListNode*)sList; 116 CircleListNode* first = sList->header.next; 117 CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - 1); 118 119 for(i=0; i<pos; i++) 120 { 121 current = current->next; 122 } 123 124 ret = current->next; 125 current->next = ret->next; 126 127 sList->length--; 128 129 if( first == ret ) 130 { 131 sList->header.next = ret->next; 132 last->next = ret->next; 133 } 134 135 if( sList->slider == ret ) 136 { 137 sList->slider = ret->next; 138 } 139 140 if( sList->length == 0 ) 141 { 142 sList->header.next = NULL; 143 sList->slider = NULL; 144 } 145 } 146 147 return ret; 148 } 149 150 CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) // O(n) 151 { 152 TCircleList* sList = (TCircleList*)list; 153 CircleListNode* ret = NULL; 154 int i = 0; 155 156 if( sList != NULL ) 157 { 158 CircleListNode* current = (CircleListNode*)sList; 159 160 for(i=0; i<sList->length; i++) 161 { 162 if( current->next == node ) 163 { 164 ret = current->next; 165 break; 166 } 167 168 current = current->next; 169 } 170 171 if( ret != NULL ) 172 { 173 CircleList_Delete(sList, i); 174 } 175 } 176 177 return ret; 178 } 179 180 CircleListNode* CircleList_Reset(CircleList* list) // O(1) 181 { 182 TCircleList* sList = (TCircleList*)list; 183 CircleListNode* ret = NULL; 184 185 if( sList != NULL ) 186 { 187 sList->slider = sList->header.next; 188 ret = sList->slider; 189 } 190 191 return ret; 192 } 193 194 CircleListNode* CircleList_Current(CircleList* list) // O(1) 195 { 196 TCircleList* sList = (TCircleList*)list; 197 CircleListNode* ret = NULL; 198 199 if( sList != NULL ) 200 { 201 ret = sList->slider; 202 } 203 204 return ret; 205 } 206 207 CircleListNode* CircleList_Next(CircleList* list) // O(1) 208 { 209 TCircleList* sList = (TCircleList*)list; 210 CircleListNode* ret = NULL; 211 212 if( (sList != NULL) && (sList->slider != NULL) ) 213 { 214 ret = sList->slider; 215 sList->slider = ret->next; 216 } 217 218 return ret; 219 }
1 #ifndef _CIRCLELIST_H_ 2 #define _CIRCLELIST_H_ 3 4 typedef void CircleList; 5 typedef struct _tag_CircleListNode CircleListNode; 6 struct _tag_CircleListNode 7 { 8 CircleListNode* next; 9 }; 10 11 CircleList* CircleList_Create(); 12 13 void CircleList_Destroy(CircleList* list); 14 15 void CircleList_Clear(CircleList* list); 16 17 int CircleList_Length(CircleList* list); 18 19 int CircleList_Insert(CircleList* list, CircleListNode* node, int pos); 20 21 CircleListNode* CircleList_Get(CircleList* list, int pos); 22 23 CircleListNode* CircleList_Delete(CircleList* list, int pos); 24 25 CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node); 26 27 CircleListNode* CircleList_Reset(CircleList* list); 28 29 CircleListNode* CircleList_Current(CircleList* list); 30 31 CircleListNode* CircleList_Next(CircleList* list); 32 33 #endif
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "CircleList.h" 4 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 7 struct Value 8 { 9 CircleListNode header; 10 int v; 11 }; 12 13 int main(int argc, char *argv[]) 14 { 15 int i = 0; 16 CircleList* list = CircleList_Create(); 17 18 struct Value v1; 19 struct Value v2; 20 struct Value v3; 21 struct Value v4; 22 struct Value v5; 23 struct Value v6; 24 struct Value v7; 25 struct Value v8; 26 27 v1.v = 1; 28 v2.v = 2; 29 v3.v = 3; 30 v4.v = 4; 31 v5.v = 5; 32 v6.v = 6; 33 v7.v = 7; 34 v8.v = 8; 35 36 CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list)); 37 CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list)); 38 CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list)); 39 CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list)); 40 41 CircleList_Insert(list, (CircleListNode*)&v5, 5); 42 CircleList_Delete(list, 0); 43 44 for(i=0; i<2*CircleList_Length(list); i++) 45 { 46 struct Value* pv = (struct Value*)CircleList_Get(list, i); 47 48 printf("%d\\n", pv->v); 49 } 50 51 printf("\\n"); 52 53 while( CircleList_Length(list) > 0 ) 54 { 55 struct Value* pv = (struct Value*)CircleList_Delete(list, 0); 56 57 printf("%d\\n", pv->v); 58 } 59 60 printf("\\n"); 61 62 CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list)); 63 CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list)); 64 CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list)); 65 CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list)); 66 CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list)); 67 CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list)); 68 CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list)); 69 CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list)); 70 71 for(i=0; i<CircleList_Length(list); i++) 72 { 73 struct Value* pv = (struct Value*)CircleList_Next(list); 74 75 printf("%d\\n", pv->v); 76 } 77 78 printf("\\n"); 79 80 CircleList_Reset(list); 81 82 while( CircleList_Length(list) > 0 ) 83 { 84 struct Value* pv = NULL; 85 86 for(i=1; i<3; i++) 87 { 88 CircleList_Next(list); 89 } 90 91 pv = (struct Value*)CircleList_Current(list); 92 93 printf("%d\\n", pv->v); 94 95 CircleList_DeleteNode(list, (CircleListNode*)pv); 96 } 97 98 CircleList_Destroy(list); 99 100 return 0; 101 }
以上是关于循环链表的主要内容,如果未能解决你的问题,请参考以下文章