当前位置:编程学习 > VC++ >>

在C语言中大概实现VC++中的LISTARRAY功能方法(二)

上一篇:http://www.zzzyk.com/kf/201206/136582.html


1. #ifndef __LISTARRAY_H__ 
2. #define __LISTARRAY_H__ 
3. #include "rtthread.h" 
4. #include "finsh.h" 
5.  
6. #define LISTDEFAULTSIZE     20 
7. #define LISTCHANGESIZE      10 
8.  
9. //LIST数组 
10. typedef struct _List List; 
11. struct _List 
12. { 
13.     void **pListPointArray;                         //LIST数组指针 
14.     int Total;                                      //元素个数 
15.     int ChangeSize;                                 //每次改变容量的时候容量的大小 
16.     int Size;                                       //当前容量 
17.     void (*Add)(List *pList, void *pListPoint);     //添加 
18.     void (*Remove)(List *pList, void *pListPoint);  //移除 
19.     void (*Delete)(void *pList);                    //析构 
20. }; 
21. //List类的析构函数 
22. static void ListDelete(void *pList) 
23. { 
24.     rt_free(((List *)pList)->pListPointArray); 
25.     rt_free(pList);                                     //再释放整个List类 
26. } 
27. //元素增加函数 
28. static void ListAdd(List *pList, void *pListPoint) 
29. { 
30.     void **tListPointArray; 
31.     if(pList->Size == pList->Total)                     //如果空间已满 
32.     { 
33.         pList->Size = pList->Size + pList->ChangeSize;  //改变空间的尺寸 
34.         tListPointArray = rt_malloc(sizeof(int *) * pList->Size);     //重新申请内存 
35.         memcpy(tListPointArray, pList->pListPointArray, sizeof(int *) * pList->Total); //将原内存的数据拷到新内存 
36.         rt_free(pList->pListPointArray);                //释放原空间 
37.         pList->pListPointArray = tListPointArray;       //将新空间指针代替原空间指针 
38.     } 
39.     pList->pListPointArray[pList->Total] = pListPoint;         //将添加的元素放到最后一个存储单元中 
40.     pList->Total++;                                     //个数加1 
41. } 
42. //元素移除函数 
43. static void ListRemove(List *pList, void *pListPoint) 
44. { 
45.     int pListIndex, tListIndex; 
46.     void **tListPointArray; 
47.     if(pList->Total == 0) return;                                       //总数为0时退出 
48.     for(pListIndex = 0, tListIndex= 0; pListIndex < pList->Total; pListIndex++) //查找移除点 
49.     { 
50.         if(pList->pListPointArray[pListIndex] != pListPoint)         //当前点不是移除点 
51.         { 
52.             pList->pListPointArray[tListIndex] = pList->pListPointArray[pListIndex];     //拷贝 
53.             tListIndex++;                                               //拷贝序号加1 
54.         } 
55.     } 
56.     pList->Total = tListIndex; 
57.      
58.     if(pList->Total <= (pList->Size - pList->ChangeSize)) 
59.     { 
60.         pList->Size = pList->Size - pList->ChangeSize;              //改变内存尺寸 
61.         tListPointArray = rt_malloc(sizeof(int *) * pList->Size);    //申请新内存 
62.         memcpy(tListPointArray, pList->pListPointArray, sizeof(int *) * pList->Total);//拷贝数据 
63.         rt_free(pList->pListPointArray);                //释放原空间 
64.         pList->pListPointArray = tListPointArray;       //将新空间指针代替原空间指针 
65.     } 
66. } 
67. //List构造函数 
68. static List *ListCreate(void) 
69. { 
70.     List *pList = (List *)rt_malloc(sizeof(List)); 
71.     pList->Total = 0; 
72.     pList->Size = LISTDEFAULTSIZE; 
73.     pList->ChangeSize = LISTCHANGESIZE; 
74.     pList->pListPointArray = rt_malloc(LISTDEFAULTSIZE); 
75.     pList->Add = ListAdd; 
76.    

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,