头文件:
1 #ifndef _SEQSTACK_H_ 2 #define _SEQSTACK_H_ 3 4 5 typedef void SeqStack; 6 //创建一个栈 7 SeqStack* SeqStack_Create(int capacity); 8 //删除一个栈 9 void SeqStack_Destroy(SeqStack* stack); 10 //清空一个栈 11 void SeqStack_Clear(SeqStack* stack); 12 //入栈 13 int SeqStack_Push(SeqStack* stack, void* item); 14 //出栈 15 void* SeqStack_Pop(SeqStack* stack); 16 //获取栈顶元素 17 void* SeqStack_GetTop(SeqStack* stack); 18 //获取栈的长度 19 int SeqStack_GetSize(SeqStack* stack); 20 //获取栈的容量 21 int SeqStack_Capacity(SeqStack* stack); 22 23 24 25 #endif
CPP文件:
1 #include "circleList.h" 2 #include <iostream> 3 4 using namespace std; 5 6 //这个为头链表头 7 typedef struct _tag_CircleList 8 { 9 CircleListNode header; 10 CircleListNode* slider; 11 int length; 12 }tagCircleList; 13 14 //创建一个循环链表 15 CircleList* CircleList_Create() 16 { 17 tagCircleList* ret = (tagCircleList*)malloc(sizeof(tagCircleList)); //分配内存 18 if (ret == NULL) 19 { 20 return NULL; 21 } 22 23 //初始化 24 ret->header.next = NULL; 25 ret->length = 0; 26 ret->slider = NULL; 27 28 return ret; 29 } 30 31 //删除一个循环链表 32 void CircleList_Destroy(CircleList* list) 33 { 34 if (list = NULL) 35 { 36 return; 37 } 38 //释放内存 39 free(list); 40 return; 41 } 42 43 //清空一个循环链表 44 void CircleList_Clear(CircleList* list) 45 { 46 tagCircleList* sList = NULL; 47 sList = (tagCircleList*)list; 48 if (sList == NULL) 49 { 50 return ; 51 } 52 //重置为初始化状态 53 sList->header.next = NULL; 54 sList->length = 0; 55 sList->slider = NULL; 56 return; 57 } 58 59 //返回链表的长度 60 int CircleList_Length(CircleList* list) 61 { 62 tagCircleList* sList = NULL; 63 sList = (tagCircleList*)list; 64 int ret = -1; 65 //异常处理 66 if (list == NULL) 67 { 68 return ret; 69 } 70 71 return sList->length; 72 } 73 74 //在POS位置插入一个节点 75 int CircleList_Insert(CircleList* list, CircleListNode* node, int pos) 76 { 77 tagCircleList* sList = NULL; 78 sList = (tagCircleList*)list; 79 int ret = -1; 80 //异常处理 81 if(list == NULL || node == NULL || pos<0) 82 { 83 return ret; 84 } 85 //临时变量Current 86 CircleListNode* Current = (CircleListNode*)sList; 87 88 for(int i = 0; (i < pos) && (Current->next != NULL); i++) 89 { 90 Current = Current->next; 91 } 92 93 node->next = Current->next; 94 Current->next = node; 95 96 //当长度为0时 游标指向node 97 if (sList->length == 0) 98 { 99 sList->slider = node; 100 } 101 102 sList->length++; 103 //如果current 依旧指向链表头 证明没跳走 是从0开始插入的 需要头插法 104 if (Current == (CircleListNode*)sList) 105 { 106 //定义一个辅助last 变量来获取尾部节点的信息 107 CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - 1); 108 //将尾部节点的NEXT域存为当前节点(头节点) 109 last->next = Current->next; 110 } 111 return 0; 112 } 113 114 //获取POS位置节点的信息 115 CircleListNode* CircleList_Get(CircleList* list, int pos) 116 { 117 118 tagCircleList* sList = (tagCircleList*)list; 119 CircleListNode* ret = NULL; 120 int i = 0; 121 if (list == NULL || pos < 0) 122 { 123 return NULL; 124 } 125 CircleListNode* Current = (CircleListNode*)sList; 126 for(i = 0; i < pos; i++) 127 { 128 Current = Current->next; 129 } 130 131 ret = Current->next; 132 return ret; 133 } 134 135 //删除POS位置的节点 136 CircleListNode* CircleList_Delete(CircleList* list, int pos) 137 { 138 tagCircleList* sList = (tagCircleList*)list; 139 CircleListNode* ret = NULL; 140 141 if ((sList != NULL) && (pos >=0) && (sList->length > 0)) 142 { 143 //将Current指向表头 144 CircleListNode* Current = (CircleListNode*)(&(sList->header)); 145 //辅助节点last 进行头节点的删除使用 存取最后一个元素 146 CircleListNode* last = NULL; 147 148 for(int i = 0; i < pos; i++) 149 { 150 Current = Current->next; 151 } 152 //删除头结点 153 if ( Current == (CircleListNode*)sList) 154 { 155 last = (CircleListNode*)CircleList_Get(sList, sList->length - 1); 156 } 157 //要删除的元素 158 ret = Current->next; 159 Current->next = ret->next; 160 sList->length--; 161 162 //判断链表非空 163 if( last != NULL) 164 { 165 //sList->header.next = ret->next; 166 Current->next = ret->next; 167 last->next = ret->next; 168 } 169 //若删除的元素为游标所指的元素 170 if(sList->slider = ret) 171 { 172 sList->slider = ret->next; 173 } 174 //若删除元素后 链表长度为0 做处理 175 if (sList->length == 0) 176 { 177 sList->header.next = NULL; 178 sList->slider = NULL; 179 } 180 } 181 return ret; 182 } 183 184 ////与游标相关的函数 185 //删除游标所指的位置节点 186 CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node) 187 { 188 tagCircleList* sList = (tagCircleList*)list; 189 CircleListNode* ret = NULL; 190 int i = 0; 191 192 if (sList != NULL) 193 { 194 CircleListNode* Current = (CircleListNode*)sList; 195 //循环查找node 在链表中的位置 196 for (i = 0; i < sList->length; i++) 197 { 198 if (Current->next == node) 199 { 200 ret = Current->next; 201 break; 202 } 203 204 Current = Current->next; 205 } 206 //找到了 使用CircleList_Delete 删除 207 if(ret != NULL) 208 { 209 CircleList_Delete(list, i); 210 } 211 212 } 213 214 return ret; 215 } 216 217 //重置游标位置 218 CircleListNode* CircleList_Reset(CircleList* list) 219 { 220 tagCircleList* sList = (tagCircleList*)list; 221 CircleListNode* ret = NULL; 222 //如果不为空 223 if (sList != NULL) 224 { 225 sList->slider = sList->header.next; 226 ret = sList->slider; 227 } 228 229 return ret; 230 } 231 232 //当前游标位置 233 CircleListNode* CircleList_Current(CircleList* list) 234 { 235 tagCircleList* sList = (tagCircleList*)list; 236 CircleListNode* ret = NULL; 237 //如果不为空 238 if (sList != NULL) 239 { 240 ret = sList->slider; 241 } 242 243 return ret; 244 } 245 246 //把游标位置返回,游标下移 247 CircleListNode* CircleList_Next(CircleList* list) 248 { 249 tagCircleList* sList = (tagCircleList*)list; 250 CircleListNode* ret = NULL; 251 //如果不为空 252 if((sList != NULL) && (sList->slider != NULL)) 253 { 254 ret = sList->slider; 255 sList->slider = ret->next; 256 } 257 258 return ret; 259 }
测试函数:
1 #include "circleList.h" 2 #include <iostream> 3 4 using namespace std; 5 6 typedef struct _Temp_Test 7 { 8 CircleListNode node; 9 int temp; 10 char temp2; 11 }TempTast; 12 13 14 15 16 int main() 17 { 18 CircleList* circlelist = NULL; 19 20 circlelist = CircleList_Create(); 21 //异常处理 22 if (circlelist == NULL) 23 { 24 cout << "Create Err " << endl; 25 return -1; 26 } 27 28 TempTast t1, t2, t3, t4, t5; 29 t1.temp = 1; 30 t2.temp = 2; 31 t3.temp = 3; 32 t4.temp = 4; 33 t5.temp = 5; 34 //插入元素 35 CircleList_Insert(circlelist, (CircleListNode*)(&t1), 0); 36 CircleList_Insert(circlelist, (CircleListNode*)(&t2), 0); 37 CircleList_Insert(circlelist, (CircleListNode*)(&t3), 0); 38 CircleList_Insert(circlelist, (CircleListNode*)(&t4), 0); 39 CircleList_Insert(circlelist, (CircleListNode*)(&t5), 0); 40 //测试功能 41 cout << "Length: " << CircleList_Length(circlelist) << endl; 42 //遍历两次 43 cout << "遍历两次:" << endl; 44 for(int i = 0; i < 2*CircleList_Length(circlelist); i++) 45 { 46 cout <<"Node:" << ((TempTast*)CircleList_Get(circlelist, i))->temp << endl; 47 } 48 cout << endl; 49 //删除第一个节点 50 cout <<"Node:" << ((TempTast*)CircleList_Delete(circlelist, 0))->temp << endl; 51 //清空 52 CircleList_Clear(circlelist); 53 cout << "After Clear Length: " << CircleList_Length(circlelist) << endl; 54 55 //插入元素 56 CircleList_Insert(circlelist, (CircleListNode*)(&t1), 0); 57 CircleList_Insert(circlelist, (CircleListNode*)(&t2), 0); 58 CircleList_Insert(circlelist, (CircleListNode*)(&t3), 0); 59 CircleList_Insert(circlelist, (CircleListNode*)(&t4), 0); 60 CircleList_Insert(circlelist, (CircleListNode*)(&t5), 0); 61 //删除指定元素 62 cout << "Delete Node :" << ((TempTast*)CircleList_DeleteNode(circlelist, (CircleListNode*)(&t1)))->temp << endl; 63 //显示游标当前位置 64 cout << "Silder Now :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl; 65 //移动后 66 CircleList_Next(circlelist); 67 cout << "Silder After Next :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl; 68 //重置后 69 CircleList_Reset(circlelist); 70 cout << "Silder After Reset :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl; 71 cout << endl; 72 //销毁 73 CircleList_Destroy(circlelist); 74 cout << "circle has been Destroied" << endl; 75 system("pause"); 76 return 0; 77 }