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

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

1. #ifndef __LISTARRAY_H__ 
2. #define __LISTARRAY_H__ 
3. #include "rtthread.h" 
4. #include "finsh.h" 
5. //LIST数组 
6. typedef struct _ListArray ListArray; 
7. struct _ListArray 
8. { 
9.     void **pListPointArray;                         //LIST数组指针 
10.     int Total;                                      //元素个数 
11.     void (*Add)(ListArray *pList, void *pListPoint);     //添加 
12.     void (*Remove)(ListArray *pList, void *pListPoint);  //移除 
13.     void (*Delete)(void *pList);                    //析构 
14. }; 
15. //List类的析构函数 
16. static void ListDelete(void *pList) 
17. { 
18.     if(((ListArray *)pList)->pListPointArray != RT_NULL)     //先释放指针数组 
19.     { 
20.         rt_free(((ListArray *)pList)->pListPointArray); 
21.     } 
22.     rt_free(pList);                                     //再释放整个List类 
23. } 
24. //元素增加函数 
25. static void ListAdd(ListArray *pList, void *pListPoint) 
26. { 
27.     void **tListPointArray = rt_malloc(sizeof(int *) * (pList->Total + 1));     //申请比原来大一个存储单元的内存 
28.     int pListIndex; 
29.     for(pListIndex = 0; pListIndex < pList->Total; pListIndex++)        //拷贝 
30.     { 
31.         if(pList->pListPointArray[pListIndex] == pListPoint)                 //判断,如果有相同的元素存在 
32.         {    
33.             rt_free(tListPointArray);                                        //释放现申请的内存 
34.             return;                                                     //返回 
35.         } 
36.         tListPointArray[pListIndex] = pList->pListPointArray[pListIndex];         //拷贝 
37.     } 
38.     tListPointArray[pList->Total] = pListPoint;                              //将添加的元素放到最后一个存储单元中 
39.     pList->Total += 1;                                                  //总数加1 
40.     if(pList->pListPointArray != RT_NULL) rt_free(pList->pListPointArray);                      //释放原来的内存 
41.     pList->pListPointArray = tListPointArray;                                            //将新的句柄替换原句柄 
42. } 
43. //元素移除函数 
44. static void ListRemove(ListArray *pList, void *pListPoint) 
45. { 
46.     int pListIndex, tListIndex; 
47.     void **tListPointArray; 
48.     void **FreePointArray; 
49.     void **SavePointArray; 
50.     if(pList->Total == 0) return;                                       //总数为0时退出 
51.     tListPointArray = rt_malloc(sizeof(int) * (pList->Total - 1));    //申请比原来小一个存储单元的内存 
52.     FreePointArray = tListPointArray;                                  //将刚申请的内存空间作为默认的释放空间 
53.     SavePointArray = pList->pListPointArray;                           //将已有的内存空间作为默认的存储空间 
54.     for(pListIndex = 0, tListIndex= 0; pListIndex < pList->Total; pListIndex++) //查找移除点 
55.     { 
56.         if(pList->pListPointArray[pListIndex] == pListPoint)         //当前点是移除点 
57.         { 
58.             FreePointArray = pList->pListPointArray;            //改变释放内存指针 
59.             SavePointArray = tListPointArray;                   //改变保留内存指针 
60.             continue;                                           //结束本次循环 
61.         } 
62.         if(tListIndex < (pList->Total -1))                      //如果当前点不是移除点,拷贝序号小于总量减1 
63.         { 
64.             tListPointArray[tListIndex] = pList->pListPointArray[pListIndex];     //拷贝 
65.             tListIndex++;                                               //拷贝序号加1 
66.         } 
67.     } 
68.     pList->Total = (SavePointArray == tListPointArray) ? pList->Total - 1 : pList->Total;   //根据保留的内存块改变总数的值 
69.     if(FreePointArray != RT_NULL) rt_free(FreePointArray);        //释放该释放的不用的内存块 
70.     pList->pListPointArray = SavePointArray; //保留该保留的 
71. } 
72. //List构造函数 
73. static ListArray *ListCreate(void) 
74. { 
75.     ListArray *pList = (ListArray *)rt_malloc(sizeof(ListArray)); 
76.     pList->Total = 0; 
77.     pList->pListPointArray = RT_NULL; 
78.     pList->Add = ListAdd; 
79.     pList->Remove = ListRemove; 
80.     pList->Delete = ListDelete; 
81.     return pList; 
82. } 
83. #endif 

此种方法是在添加或删除数组中的元素时,重新申请大1或者小1的一块内存,然后将原数组拷到新申请的内存中,然后将原来的数组指针替换掉!
作者:sx_wpc

图片内容