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

来源:岁月联盟 编辑:猪蛋儿 时间:2012-06-21

上一篇:http://www.2cto.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.     pList->Remove = ListRemove; 
77.     pList->Delete = ListDelete; 
78.     return pList; 
79. } 
80. #endif 

此方法是由方法1发展而来,在方法1中添加了两个参数,一个是SIZE参数,一个是CHANGESIZE参数,SIZE参数是初始化时的数组容量,CHANGESIZE是在数据刚好达到SIZE时将数组容量改变的数组添加或减少的容量。比如初始大小为SIZE,然后当数组增加到SIZE的时候要存储第SIZE + 1个数据的时候就重新申请一块比SIZE大CHANGESIZE的内存,然后再将数据转移进去,将原内存释放,将新内存的指针保存。减少是一个道理的,当减到SIZE的时候,将原尺寸减个CHANGESIZE,然后申请内存,转移数据,释放老内存,保存新指针。
此方法的好处是不用每次都申请、释放内存、转移数据,操作LIST比较频繁的时候,用此方法比用第1种方法在速度上有优势!

 

作者:sx_wpc

图片内容