回调函数应用
1#include <iostream>
2using namespace std;
3
4#define MAX 5
5
6typedef struct tagNode_t
7{
8 struct tagNode_t *pPre;
9 struct tagNode_t *pNex;
10 void * pData;
11}Node_t;
12
13typedef struct tagList_t
14{
15 Node_t pHead;
16}List_t;
17
18
19typedef enum tagStatusEnum
20{
21 LIST_NG = 0,
22 LIST_OK,
23}StatusEnum;
24
25//函数指针
26typedef StatusEnum (*pFun ) (void* data);
27typedef StatusEnum (*pVistFun)(void* ctx, void* data);
28
29StatusEnum CreateList(Node_t* p , int* i);
30StatusEnum DeleteList(List_t* p);
31
32StatusEnum PrintList(List_t* list, pFun print);
33StatusEnum PrintStatus(void* p);
34
35StatusEnum List_ForEach(List_t* p, pVistFun, void* pvCtx);
36StatusEnum FindMax(void* pvCtx, void* pvData);
37StatusEnum Sum(void* pvCtx, void* pvData);
38
39
40StatusEnum ListForEach(List_t* p, pVistFun Fun, void* pvCtx)
41{
42 StatusEnum Status = LIST_OK;
43 Node_t *pstTemp = (p->pHead).pNex;
44 while((NULL != pstTemp ) && (Status != LIST_NG))
45 {
46 Status = Fun(pvCtx, pstTemp->pData);
47 pstTemp = pstTemp->pNex;
48 }
49
50 return Status;
51}
52
53
54StatusEnum Sum(void* pvCtx, void* pvData)
55{
56 int *piCtx = (int*)pvCtx;
57 int *piData = (int*)pvData;
58 *piCtx += *piData;
59 return LIST_OK;
60}
61
62StatusEnum FindMax(void* pvCtx, void* pvData)
63{
64 int *piMax = (int*)pvCtx;
65 int *piData = (int*)pvData;
66 if ( *piMax < *piData)
67 {
68 *piMax = *piData;
69 }
70
71 return LIST_OK;
72}
73
74
75StatusEnum CreateList(Node_t *p , int *i)
76{
77 Node_t *pTemp = new Node_t;
78 pTemp->pPre = NULL;
79 pTemp->pNex = NULL;
80 pTemp->pData = i;
81
82 //Head Insert
83 pTemp->pNex = (p)->pNex;
84 (p)->pNex = pTemp;
85 pTemp->pPre = p;
86
87 if (NULL != pTemp->pNex)
88 {
89 pTemp->pNex->pPre = pTemp;
90 }
91
92 return LIST_OK;
93}
94
95StatusEnum PrintList(List_t *p, pFun print)
96{
97 Node_t *pTemp = p->pHead.pNex;
98 while ( NULL != pTemp)
99 {
100 print(pTemp->pData);
101 pTemp = pTemp->pNex;
102 }
103
104 return LIST_OK;
105}
106
107
108StatusEnum PrintStatus(void* p)
109{
110 //cout<<"A "<<(int) (*p);
111 cout<<"B "<<*(int*)p<<endl;
112 return LIST_OK;
113}
114
115
116StatusEnum DeleteList(List_t *p)
117{
118 Node_t *pTemp = p->pHead.pNex;
119 Node_t *pCur = NULL;
120 while ( NULL != pTemp)
121 {
122 pCur = pTemp;
123 pTemp = pTemp->pNex;
124 delete pCur;
125 }
126
127 return LIST_OK;
128}
129
130int main(int argc, char ** argv)
131{
132 List_t list;
133 (list.pHead).pPre = NULL;
134 (list.pHead).pNex = NULL;
135 (list.pHead).pData = NULL;
136
137 //Create
138 int aiData[MAX] = {1,3,4,5,2};
139 for (int i = 0; i < MAX; ++ i)
140 {
141 CreateList(&(list.pHead), &(aiData[i]));
142 }
143
144
145 //Print
146 PrintList(&list, PrintStatus);
147
148 //Max
149 int iMax = 0;
150 ListForEach(&list, FindMax, &iMax);
151 cout<<"Max: "<<iMax<<endl;
152
153 //Sum
154 int iSum = 0;
155 ListForEach(&list, Sum, &iSum);
156 cout<<"Sum: "<<iSum<<endl;
157
158 //Delete
159 DeleteList(&list);
160
161 return 0;
162}
163
164
165 好郁闷的void* int*
StatusEnum CreateList(Node_t *p , void *i);StatusEnum PrintStatus(void* p)
操作:
构造的时候
for (int i = 0; i < 5; ++ i)
{
CreateList(&((list.pHead)), (void*)i);
}
打印的时候
StatusEnum PrintStatus(void* p)
{
//cout<<"A "<<(int) (*p);
// cout<<"B "<<*((int*)p)<<endl;
cout<<(int)p<<endl; return LIST_OK;
}
就这么点代码,纠结死我了,why? 为什么这样就不用
考虑生命周期了呢,为什么打印不是
cout<<"B "<<*((int*)p)<<endl;而是
cout<<(int)p<<endl;