c语言实现通用数据结构(一):通用链表

   忽然想起来,大概在两年之前学习C语言的时候,曾经用C语言写过一些通用的数据结构。主要也就实现了链表、队列、椎、HashSet,还有HashMap。当时只是知道标准的C语言中没有这方面的类库,后来才知道有很多第三方的类似这样的类库。废话不多说,先把代码粘过来。

下面实现的是通用链表,注意链表中只存储了指针,没有储存实际的数据。

头文件

[cpp]  view plain  copy
  1. /************************* 
  2. *** File myList.h 
  3. **************************/  
  4.   
  5. #ifndef MYLIST_H_INCLUDED  
  6. #define MYLIST_H_INCLUDED  
  7. #include <stdio.h>  
  8.   
  9.   
  10. typedef struct myNode  
  11. {  
  12.     void * data;  
  13.     struct myNode *next;  
  14. } MyNode;  
  15.   
  16. typedef struct myList  
  17. {  
  18.     MyNode * first;  
  19.     MyNode * last;  
  20.     int count;  
  21.     int (*equal)(void * a, void * b);  
  22. } MyList;  
  23.   
  24. typedef struct myListIterator  
  25. {  
  26.     MyNode * p;  
  27.     int count;  
  28.     int allSize;  
  29. } MyListIterator;  
  30.   
  31. //创建链表  
  32. MyList * createMyList();  
  33.   
  34. //创建链表,带有相等参数,用于查找  
  35. MyList * createMySearchList(int(*equal)(void * a, void * b));  
  36.   
  37. //释放链表  
  38. void freeMyList(MyList * list);  
  39.   
  40. //插入在尾部  
  41. void myListInsertDataAtLast(MyList* const list, voidconst data);  
  42.   
  43. //插入在首部  
  44. void myListInsertDataAtFirst(MyList * const list, voidconst data);  
  45.   
  46. //插入  
  47. void myListInsertDataAt(MyList * const list, voidconst data, int index);  
  48.   
  49. //删除在尾部  
  50. void* myListRemoveDataAtLast(MyList* const list);  
  51.   
  52. //删除在首部  
  53. void* myListRemoveDataAtFirst(MyList * const list);  
  54.   
  55. //删除  
  56. void* myListRemoveDataAt(MyList* const list, int index);  
  57.   
  58. //删除对象,返回是否删除成功  
  59. int myListRemoveDataObject(MyList* const list, void * data);  
  60.   
  61. //长度  
  62. int myListGetSize(const MyList * const list);  
  63.   
  64. //打印  
  65. void myListOutput(const MyList * const list, void(*pt)(const void * const));  
  66.   
  67. //取得数据  
  68. void* myListGetDataAt(const MyList * const list, int index);  
  69.   
  70. //取得第一个数据  
  71. void* myListGetDataAtFirst(const MyList * const list);  
  72.   
  73. //取得最后一个数据  
  74. void* myListGetDataAtLast(const MyList * const list);  
  75.   
  76. //查找某个数据的位置,如果equal方法为空,比较地址,否则调用equal方法  
  77. //如果不存在返回-1,如果存在,返回出现的第一个位置  
  78. int myListFindDataIndex(const MyList * const list, void * data);  
  79.   
  80. //创建遍历器  
  81. MyListIterator* createMyListIterator(const MyList * const list);  
  82.   
  83. //释放遍历器  
  84. void freeMyListIterator(MyListIterator* iterator);  
  85.   
  86. //遍历器是否有下一个元素  
  87. int myListIteratorHasNext(const MyListIterator* const iterator);  
  88.   
  89. //返回遍历器的下一个元素  
  90. void * myListIteratorNext(MyListIterator* const iterator);  
  91.   
  92. #endif // MYLIST_H_INCLUDED  

源文件

[cpp]  view plain  copy
  1. /************************* 
  2. *** File myList.c 
  3. **************************/  
  4.   
  5. #include "myList.h"  
  6. #include <stdlib.h>  
  7. //创建链表  
  8. MyList * createMyList()  
  9. {  
  10.     MyList * re = (MyList *) malloc(sizeof(MyList));  
  11.     re->count = 0;  
  12.     re->first = NULL;  
  13.     re->last = NULL;  
  14.     re->equal = NULL;  
  15.     return re;  
  16. }  
  17.   
  18. //释放链表  
  19. void freeMyList(MyList * list)  
  20. {  
  21.     MyNode * p;  
  22.     while (list->first)  
  23.     {  
  24.         p = list->first->next;  
  25.         free(list->first);  
  26.         list->first = p;  
  27.     }  
  28.     free(list);  
  29. }  
  30.   
  31. //插入在尾部  
  32. void myListInsertDataAtLast(MyList * const list, voidconst data)  
  33. {  
  34.     MyNode * node = (MyNode *) malloc(sizeof(MyNode));  
  35.     node->data = data;  
  36.     node->next = NULL;  
  37.     if (list->count)  
  38.     {  
  39.         list->last->next = node;  
  40.         list->last = node;  
  41.     }  
  42.     else  
  43.     {  
  44.         list->first = node;  
  45.         list->last = node;  
  46.     }  
  47.     (list->count)++;  
  48. }  
  49.   
  50. //插入在首部  
  51. void myListInsertDataAtFirst(MyList * const list, voidconst data)  
  52. {  
  53.     MyNode * node = (MyNode *) malloc(sizeof(MyNode));  
  54.     node->data = data;  
  55.     node->next = NULL;  
  56.   
  57.     if (list->count)  
  58.     {  
  59.         node->next = list->first;  
  60.         list->first = node;  
  61.     }  
  62.     else  
  63.     {  
  64.         list->first = node;  
  65.         list->last = node;  
  66.     }  
  67.     (list->count)++;  
  68. }  
  69.   
  70. //长度  
  71. int myListGetSize(const MyList * const list)  
  72. {  
  73.     return list->count;  
  74. }  
  75.   
  76. //打印  
  77. void myListOutput(const MyList * const list, void(*pt)(const void * const))  
  78. {  
  79.     MyNode * p = list->first;  
  80.     while (p)  
  81.     {  
  82.         (*pt)(p->data);  
  83.         p = p->next;  
  84.     }  
  85. }  
  86.   
  87. //删除在尾部  
  88. void* myListRemoveDataAtLast(MyList* const list)  
  89. {  
  90.     if (list->count == 1)  
  91.     {  
  92.         return myListRemoveDataAtFirst(list);  
  93.     }  
  94.     MyNode * p = list->first;  
  95.     while (p->next != list->last)  
  96.     {  
  97.         p = p->next;  
  98.     }  
  99.     void *re = list->last->data;  
  100.     free(list->last);  
  101.     p->next = NULL;  
  102.     list->last = p;  
  103.     (list->count)--;  
  104.     return re;  
  105. }  
  106.   
  107. //删除在首部  
  108. void* myListRemoveDataAtFirst(MyList * const list)  
  109. {  
  110.     MyNode *p = list->first;  
  111.     list->first = p->next;  
  112.     void * re = p->data;  
  113.     free(p);  
  114.     (list->count)--;  
  115.     if (list->count == 0)  
  116.     {  
  117.         list->last = NULL;  
  118.     }  
  119.     return re;  
  120. }  
  121.   
  122. //插入  
  123. void myListInsertDataAt(MyList * const list, voidconst data, int index)  
  124. {  
  125.     if (index == 0)  
  126.     {  
  127.         myListInsertDataAtFirst(list, data);  
  128.         return;  
  129.     }  
  130.     if (index == list->count)  
  131.     {  
  132.         myListInsertDataAtLast(list, data);  
  133.         return;  
  134.     }  
  135.     MyNode * node = (MyNode *) malloc(sizeof(MyNode));  
  136.     node->data = data;  
  137.     node->next = NULL;  
  138.   
  139.     MyNode * p = list->first;  
  140.     for (int i = 0; i < index - 1; i++)  
  141.     {  
  142.         p = p->next;  
  143.     }  
  144.     node->next = p->next;  
  145.     p->next = node;  
  146.   
  147.     (list->count)++;  
  148. }  
  149.   
  150. //删除  
  151. void* myListRemoveDataAt(MyList* const list, int index)  
  152. {  
  153.     if (index == 0)  
  154.     {  
  155.         return myListRemoveDataAtFirst(list);  
  156.     }  
  157.     if (index == list->count - 1)  
  158.     {  
  159.         return myListRemoveDataAtLast(list);  
  160.     }  
  161.   
  162.     MyNode * p = list->first;  
  163.     for (int i = 0; i < index - 1; i++)  
  164.     {  
  165.         p = p->next;  
  166.     }  
  167.     MyNode *tp = p->next;  
  168.     p->next = p->next->next;  
  169.     void * re = tp->data;  
  170.     free(tp);  
  171.     (list->count)--;  
  172.     return re;  
  173. }  
  174.   
  175. //取得数据  
  176. void* myListGetDataAt(const MyList * const list, int index)  
  177. {  
  178.     if (index == list->count - 1)  
  179.     {  
  180.         return myListGetDataAtLast(list);  
  181.     }  
  182.     MyNode * p = list->first;  
  183.     for (int i = 0; i < index; i++)  
  184.     {  
  185.         p = p->next;  
  186.     }  
  187.     return p->data;  
  188. }  
  189.   
  190. //取得第一个数据  
  191. void* myListGetDataAtFirst(const MyList * const list)  
  192. {  
  193.     return list->first->data;  
  194. }  
  195.   
  196. //取得最后一个数据  
  197. void* myListGetDataAtLast(const MyList * const list)  
  198. {  
  199.     return list->last->data;  
  200. }  
  201.   
  202. //查找某个数据的位置,如果equal方法为空,比较地址,否则调用equal方法  
  203. //如果不存在返回-1,如果存在,返回出现的第一个位置  
  204. int myListFindDataIndex(const MyList * const list, void * data)  
  205. {  
  206.     MyNode * p = list->first;  
  207.     int re = 0;  
  208.     if (list->equal)  
  209.     {  
  210.         while (p)  
  211.         {  
  212.             if (p->data == data || (*(list->equal))(p->data, data))  
  213.             {  
  214.                 return re;  
  215.             }  
  216.             re++;  
  217.             p = p->next;  
  218.         }  
  219.   
  220.     }  
  221.     else  
  222.     {  
  223.         while (p)  
  224.         {  
  225.             if (p->data == data)  
  226.             {  
  227.                 return re;  
  228.             }  
  229.             re++;  
  230.             p = p->next;  
  231.         }  
  232.     }  
  233.     return -1;  
  234. }  
  235.   
  236. //创建链表,带有相等参数,用于查找  
  237. MyList * createMySearchList(int(*equal)(void * a, void * b))  
  238. {  
  239.     MyList * re = createMyList();  
  240.     re->equal = equal;  
  241.     return re;  
  242. }  
  243.   
  244. //创建遍历器  
  245. MyListIterator* createMyListIterator(const MyList * const list)  
  246. {  
  247.     MyListIterator * re = (MyListIterator *) malloc(sizeof(MyListIterator));  
  248.     re->p = list->first;  
  249.     re->allSize = list->count;  
  250.     re->count = 0;  
  251.     return re;  
  252. }  
  253.   
  254. //释放遍历器  
  255. void freeMyListIterator(MyListIterator* iterator)  
  256. {  
  257.     free(iterator);  
  258. }  
  259.   
  260. //遍历器是否有下一个元素  
  261. int myListIteratorHasNext(const MyListIterator* const iterator)  
  262. {  
  263.     return iterator->count < iterator->allSize;  
  264. }  
  265.   
  266. //返回遍历器的下一个元素  
  267. void * myListIteratorNext(MyListIterator* const iterator)  
  268. {  
  269.     void * re = iterator->p->data;  
  270.     iterator->p = iterator->p->next;  
  271.     (iterator->count)++;  
  272.     return re;  
  273. }  
  274.   
  275. //删除对象,返回是否删除成功  
  276. int myListRemoveDataObject(MyList* const list, void * data)  
  277. {  
  278.     MyListIterator * it = createMyListIterator(list);  
  279.     int a = 0;  
  280.     while (myListIteratorHasNext(it))  
  281.     {  
  282.         void * ld = myListIteratorNext(it);  
  283.         if (data == ld || (list->equal != NULL && (*(list->equal))(ld, data)))  
  284.         {  
  285.             a = 1;  
  286.             break;  
  287.         }  
  288.     }  
  289.     if (a)  
  290.     {  
  291.         myListRemoveDataAt(list, it->count - 1);  
  292.     }  
  293.     return a;  
  294. }  

测试文件

[cpp]  view plain  copy
  1. /************************* 
  2. *** File main.c 
  3. *** test for MyList 
  4. **************************/  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include "myList.h"  
  8.   
  9. typedef struct a  
  10. {  
  11.     int i;  
  12.     char c;  
  13. } A;  
  14.   
  15. void ppt(const voidconst p)  
  16. {  
  17.     A * pp= p;  
  18.     printf("%d(%c) ", pp->i, pp->c);  
  19. }  
  20.   
  21.   
  22. int main()  
  23. {  
  24.     const int S =10;  
  25.   
  26.     //创建并初始化数据  
  27.     A * data= malloc(sizeof(A)*S);  
  28.     for (int i=0; i< S; i++)  
  29.     {  
  30.         data[i].i=i;  
  31.         data[i].c=(char)('A'+0);  
  32.     }  
  33.   
  34.     //创建链表  
  35.     MyList * list= createMyList();  
  36.   
  37.     //测试三种插入方法  
  38.     myListInsertDataAtLast( list, &data[0]);  
  39.     myListInsertDataAtFirst( list, &data[4]);  
  40.     myListInsertDataAt(list, &data[1], 1 );  
  41.   
  42.   
  43.     //测试查找  
  44.     int index = myListFindDataIndex(list, &data[2]);  
  45.     printf("%d\n", index);  
  46.     index = myListFindDataIndex(list, &data[4]);  
  47.     printf("%d\n", index);  
  48.   
  49.     //输出  
  50.     myListOutput(list, ppt );  
  51.     puts("");  
  52.   
  53.     //测试使用迭代器输出  
  54.     MyListIterator * it = createMyListIterator(list);  
  55.     while(myListIteratorHasNext(it))  
  56.     {  
  57.         A * pp = myListIteratorNext(it);  
  58.         printf("%d[%c] ", pp->i, pp->c);  
  59.     }  
  60.     puts("");  
  61.     //释放迭代器  
  62.     freeMyListIterator(it);  
  63.   
  64.     //释放链表  
  65.     freeMyList(list);  
  66.   
  67.     //释放数据  
  68.     free(data);  
  69.     return 0;  
  70. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值