Clist循环链表的实现
生活随笔
收集整理的這篇文章主要介紹了
Clist循环链表的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://github.com/liutianshx2012/Algorithms-Data_structures/tree/master/Data_structures/src2
// // Clist.h // Algorithms&Data_structures // // Created by TTc on 15-2-2. // Copyright (c) 2015年 TTc. All rights reserved. // /*** 循環鏈表*/ #ifndef __Algorithms_Data_structures__Clist__ #define __Algorithms_Data_structures__Clist__#include <stdlib.h>typedef struct CListElmt_{void *data;struct CListElmt_ *next; }CListElmt;typedef struct CList_{int size;int (*match) (const void *key1,const void *key2);void(*destroy)(void *data);CListElmt *head; }CList;void cprint_list(const CList *clist);/*Public Intefaces */ void clist_init(CList *clist, void (*destroy)(void *data));void clist_destroy(CList *clist);int clist_ins_next(CList *clist, CListElmt *element, const void *data);int clist_rem_next(CList *clist, CListElmt *element, void **data);#define clist_size(clist) ((clist) -> size) #define clist_head(clist) ((clist) -> head) #define clist_is_head(clist, element) ((element) == (clist) -> head ? 1:0) #define clist_data(element) ((element) -> data) #define clist_next(element) ((element) -> next)#endif /* defined(__Algorithms_Data_structures__Clist__) */ // // Clist.c // Algorithms&Data_structures // // Created by TTc on 15-2-2. // Copyright (c) 2015年 TTc. All rights reserved. //#include "clist.h" #include <stdio.h> #include <string.h>//O(1) void clist_init(CList *clist, void (*destroy)(void *data)){clist->size = 0;clist->destroy = destroy;clist->head = NULL; } //O(n) void clist_destroy(CList *clist){void *data;//remove each elementwhile (clist_size(clist) != 0) {if(clist_rem_next(clist, clist->head, (void**) &data) == 0 && clist->destroy != NULL){clist->destroy(data);}}//clist清除memset(clist, 0, sizeof(CList)); } /* 1: insert操作*/ /* 插入成功返回0 ,反之返回 -1 *//* 將元素插入由list指針的循環鏈表中element之后,當插入空鏈表中時,element可能指向任何位置,為了避免混淆,element此時應該設置為NULL。新的元素包含一個指向data的指針,因此只要該元素仍在鏈表中,data所引用的內存空間就應該保持合法.由調用者負責管理data所引用的存儲空間.*/ /* O(1)*/ int clist_ins_next(CList *clist, CListElmt *element, const void *data){CListElmt *new_element;if((new_element = (CListElmt*)malloc(sizeof(CListElmt))) == NULL){return -1;}new_element->data = (void*)data;//空表時候插入操作if(clist_size(clist) == 0){new_element->next = new_element;clist->head = new_element;}else{ //非空表時候的操作操作new_element->next = element->next;element->next = new_element;}//size自增clist->size++;return 0; }/* 1: remove操作:將移除由參數list指定的 循環表中element后面的元素 。2: 返回時候 data指向已經 移除元素中存儲的數據3: 由調用者負責管理于data想關聯的內存(存儲空間)*/ /* 刪除成功返回0 ,反之返回 -1 */ /* O(1)*/int clist_rem_next(CList *clist, CListElmt *element, void **data){CListElmt *old_element;//不允許從一個空鏈表 移除節點if(clist_size(clist) == 0){return -1;}*data = element->next->data;//自己指向自己 (鏈表中只有一個元素的情況)if(element->next == element){/* handle removing the last element*/old_element = element->next;clist->head = NULL;}else{/* handle removing the last element*/old_element = element->next;element->next = old_element->next;//如果要刪除的 元素 是頭元素 則需要 特殊處理 (指向 要刪除元素的 下一個位置的元素)if(old_element == clist_head(clist)){clist->head = old_element->next;}}free(old_element);clist->size --;return 0; }void cprint_list(const CList *clist) {CListElmt *element = clist_head(clist);int *data;int i = 0;if(element != NULL){do{data = clist_data(element);printf("cprint_list======>list[%d]=%d\n", i, *data);element = element->next;i++;}while (element != clist_head(clist));} } #include <string.h> #include <stdlib.h> #include <stdio.h>#include "clist.h"typedef struct Cuboid_ {int length;int width;int height; }Cuboid;Cuboid * cube_instance(const int length, const int width, const int height) {Cuboid *cb_ptr;cb_ptr = (Cuboid *)malloc(sizeof(Cuboid));if( cb_ptr == NULL )return NULL;cb_ptr->length = length;cb_ptr->width = width;cb_ptr->height = height;return cb_ptr; }/*destroy */ void destroy(void *data) {free(data);return; }/* main */ int main(int argc, char **argv) {int i;int ret = 0;CList clist;CListElmt *p = NULL;Cuboid *cb1_ptr, *cb2_ptr, *cb3_ptr, *cb4_ptr, *cb5_ptr;Cuboid *cb_ptr;//cb1_ptr ~ cb5_ptr are the data of the 5 elements.cb1_ptr = cube_instance(1,2,3);cb2_ptr = cube_instance(6,10,8);cb3_ptr = cube_instance(5,20,30);cb4_ptr = cube_instance(17,100,25);cb5_ptr = cube_instance(3,6,9);//init the clistclist_init(&clist, destroy);//insert the above 5 element /* cb1 -> cb1 */ret = clist_ins_next(&clist, NULL, (void *)cb1_ptr);if( ret != 0 ){printf("insert cb1 error\n");return -1;}/* cb1 -> cb2 -> cb1 */p = clist_head(&clist);ret = clist_ins_next(&clist, p, (void *)cb2_ptr);if( ret != 0 ){printf("insert cb2 error\n");return -1;}/* cb1 -> cb2 ->cb3 ->cb1*/p = clist_next(p);ret = clist_ins_next(&clist, p, (void *)cb3_ptr);if( ret != 0 ){printf("insert cb3 error\n");return -1;}/* cb1 -> cb2 -> cb3 -> cb4 ->cb1 */p = clist_next(p);ret = clist_ins_next(&clist, p, (void *)cb4_ptr);if( ret != 0 ){printf("insert cb4 error\n");return -1;}/* cb1 -> cb2 -> cb3 -> cb4 ->cb5 -> cb1 */p = clist_next(p);ret = clist_ins_next(&clist, p, (void *)cb5_ptr);if( ret != 0 ){printf("insert cb5 error\n");return -1;}p = clist_head(&clist); //get the head elementfor(i = 0; i < clist_size(&clist) + 1; i++ ){cb_ptr = (Cuboid *)clist_data(p); // get the element's data, every data is a Cuboid 's pointer.printf("i = %d: ",i);printf("length = %d, width = %d, height = %d\n",cb_ptr->length,cb_ptr->width,cb_ptr->height);p = clist_next(p); //pointer to next element.}//remove the head element clist_rem_next(&clist, p, (void **)&cb_ptr);printf("the removed element: length = %d, width = %d, height = %d\n",cb_ptr->length,cb_ptr->width,cb_ptr->height);destroy(cb_ptr); //free the memeory//destroy the circle list clist_destroy(&clist);printf("after destroy the list, its size = %d\n", clist_size(&clist));return 0; }總結
以上是生活随笔為你收集整理的Clist循环链表的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tendermint mempool分析
- 下一篇: 向日葵 ubuntu18