程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 在C語言中大概實現VC++中的LISTARRAY功能方法(一)

在C語言中大概實現VC++中的LISTARRAY功能方法(一)

編輯:關於C語言

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved