以下以链表为例子,仔细体会代码模块化设计的思想以及代码规范。
/*************************************************** * Coperight (C) SSE-USTC, 2014-2015 * FILE NAME : linktable.h * PRINCIPAL AUTHOR : Su YongSheng * SUBSYSTEM NAME : LinkTable * MODULE NAME : LinkTable * LANGUAGE : C++ * TARGET ENVIRONMENT : ANY * DATE OF FIRST RELEASE : 2014/4/10 * DESCRIPTION : interface of Link Table ****************************************************/ #ifndef _LINK_TABLE_H_ #define _LINK_TABLE_H_ #include <pthread.h> #define SUCCESS 0 #define FAILURE (-1) /* * LinkTable Node Type */ typedef struct LinkTableNode { struct LinkTableNode * pNext; }tLinkTableNode; /* * LinkTable Type */ typedef struct LinkTable { tLinkTableNode *pHead; tLinkTableNode *pTail; int SumOfNode; pthread_mutex_t mutex; }tLinkTable; /* * Create a LinkTable */ tLinkTable * CreateLinkTable(); /* * Delete a LinkTable */ int DeleteLinkTable(tLinkTable *pLinkTable); /* * Add a LinkTableNode to LinkTable */ int AddLinkTableNode(tLinkTable *pLinkTable ,tLinkTableNode * pNode); /* * Delete a LinkTableNode from LinkTable */ int DelLinkTableNode(tLinkTable *pLinkTable ,tLinkTableNode * pNode); /* * Get LinkTableHead */ tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable); /* * get next LinkTableHead */ tLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable ,tLinkTableNode * pNode); #endif /* _LINK_TABLE_H_ */
/*************************************************** * Coperight (C) SSE-USTC, 2014-2015 * FILE NAME : testlinktable.c * PRINCIPAL AUTHOR : Su YongSheng * SUBSYSTEM NAME : LinkTable * MODULE NAME : LinkTable * LANGUAGE : C++ * TARGET ENVIRONMENT : ANY * DATE OF FIRST RELEASE : 2014/4/10 * DESCRIPTION : test of LinkTable ****************************************************/ /* * Revision log: * Creted by Su Yongsheng,2014/4/10 * Verified callback and head/next for search by Su Yongsheng,2014 */ #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <linktable.h> #define debug typedef struct Node { tLinkTable * pNext; int data; }tNode; tNode * Search(tLinkTable *pLinkTable); int main() { int i; tLinkTable * pLinkTable = CteateLinkTable(); if(tLinkTable == NULL) { printf("CreateLinkTable Error! "); exit(0); } for(i = 0; i < 10; i++) { tNode* pNode = (tNode*)malloc(sizeof(tNode)); pNode->data = i; debug("AddLinkTableNode "); AddLinkTableNode(pLinkTable, (tLinkTableNode *)pNode); } /*search one by one*/ tNode *pTempNode = Search(pLinkTable); printf("%d ", pTempNode->data); debuf("DelLinkTableNode "); DelLinkTableNode(pLinkTable,(tLinkTableNode *)pTempNode); free(pTempNode); free(pTempNode); DelteLinkTable(pLinkTable); } tNode * Search(tLinkTable *pLinkTable) { debug("Search GetLinkTableHead "); tNode * pNode = (tNode*)GetLinkTableHead(pLinkTable); while(pNode != NULL) { if(pNode->data == 5) { return pNode; } degut(GetNextLinkTableNode "); pNode = (tNode*)GetNextLinkTableNode(pLinkTable); } return NULL; }