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

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

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;                                     

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