临时测试的链表小程序!

#include <stdio.h> #include <string.h> #include <malloc.h> typedef struct tagList { unsigned int dwType; unsigned int dwUid; void *pvData; unsigned int dwLen; struct tagList *ptNext; }TList; typedef unsigned int STATUS; #define LIST_TYPE_CHAR 1 #define LIST_TYPE_STRING 2 #define LIST_TYPE_INTEGER 3 #define LIST_TYPE_BINARY 4 /* list ok and error no */ #define LIST_OK 0 #define LIST_ERR_MALLOC 10 #define LIST_ERR_INSTERT 11 #define LIST_ERR_DELETE 12 #define LIST_ERR_DATA_MALLOC 13 #define LIST_ERR_IS_EMPTY 14 #define LIST_ERR_DEL_NO_FIND 15 #define LIST_ERR_PRT_NO_FIND 16 #define LIST_STRUCT_LEN (sizeof(TList)) TList *g_sptListHead = NULL; TList *g_sptListTail = NULL; static unsigned int ListGetNum(TList **pptList) { unsigned int dwNum = 0; TList *ptList = (*pptList); while (NULL != ptList) { ptList = ptList->ptNext; dwNum++; } return dwNum; } static STATUS ListNew(TList **pptList) { (*pptList) = (TList *)malloc(LIST_STRUCT_LEN); if (NULL == (*pptList)) { return LIST_ERR_MALLOC; } memset((*pptList), 0, LIST_STRUCT_LEN); return LIST_OK; } static STATUS ListDestory(TList **pptList) { TList *ptList = (*pptList); if (NULL == ptList) { return LIST_ERR_IS_EMPTY; } while (ptList) { ptList = (*pptList)->ptNext; free(*pptList); (*pptList) = ptList; } g_sptListHead = g_sptListTail = NULL; return LIST_OK; } static STATUS ListDelete(TList **pptList, unsigned int dwUid) { int nIsFind = 0; TList *ptList = (*pptList); TList *ptListPre = ptList; while (ptList) { if (ptList->dwUid == dwUid) { nIsFind = 1; break; } ptListPre = ptList; ptList = ptList->ptNext; } if (0 == nIsFind) { return LIST_ERR_DEL_NO_FIND; } if (ptList == g_sptListHead) { g_sptListHead = g_sptListHead->ptNext; } else { ptListPre->ptNext = ptList->ptNext; } free(ptList); ptList = NULL; return LIST_OK; } static STATUS ListInit(TList **ptList) { STATUS nState = ListNew(&(*ptList)); if (LIST_OK != nState) { return nState; } g_sptListHead = g_sptListTail = *ptList; return LIST_OK; } static STATUS ListAdd(TList **ptList, unsigned int dwType, unsigned int dwUid, void *pvBuf, unsigned int dwLen) { STATUS nState = ListNew(&(*ptList)); if (LIST_OK != nState) { return nState; } if (NULL == g_sptListHead) { g_sptListHead = *ptList;; } g_sptListTail = *ptList; (*ptList)->dwType = dwType; (*ptList)->dwUid = dwUid; (*ptList)->dwLen = dwLen; (*ptList)->pvData = (unsigned char *)malloc(dwLen+1); if (NULL == (*ptList)->pvData) { return LIST_ERR_DATA_MALLOC; } memset((*ptList)->pvData, 0, dwLen); memcpy((*ptList)->pvData, pvBuf, dwLen); return LIST_OK; } static STATUS PutChar(TList **pptList, unsigned int dwUid, char chData) { return ListAdd(&(*pptList), LIST_TYPE_CHAR, dwUid, &chData, sizeof(char)); } static STATUS PutString(TList **pptList, unsigned int dwUid, char *pchStr) { return ListAdd(&(*pptList), LIST_TYPE_STRING, dwUid, pchStr, strlen(pchStr)); } static STATUS PutInteger(TList **pptList, unsigned int dwUid, int nNum) { return ListAdd(&(*pptList), LIST_TYPE_INTEGER, dwUid, &nNum, sizeof(int)); } static STATUS PutBinary(TList **pptList, unsigned int dwUid, unsigned char *pchBinary, unsigned int dwLen) { return ListAdd(&(*pptList), LIST_TYPE_BINARY, dwUid, pchBinary, dwLen); } static void PrintListData(TList *ptList) { char chData = 0; int nData = 0; if (NULL == ptList) { return; } switch (ptList->dwType) { case LIST_TYPE_CHAR: memcpy(&chData, ptList->pvData, sizeof(char)); printf("list type is Char:%c/n", chData); break; case LIST_TYPE_STRING: printf("list type is String:%s/n", (char *)(ptList->pvData)); break; case LIST_TYPE_INTEGER: memcpy(&nData, ptList->pvData, sizeof(int)); printf("list type is Integer:%d/n", nData); break; case LIST_TYPE_BINARY: printf("list type is Binary:%d, len:%d/n", (char *)(ptList->pvData), ptList->dwLen); break; default: printf("list type is unknow[%d]/n", ptList->dwType); break; } } static STATUS PrintAllListData(TList **pptList, unsigned int dwUid) { int nIsFind = 0; TList *ptList = (*pptList); while (ptList) { if (ptList->dwUid == dwUid) { nIsFind = 1; break; } ptList = ptList->ptNext; } if (0 == nIsFind) { printf("no find uid:%d/n", dwUid); return LIST_ERR_PRT_NO_FIND; } else { printf("find uid:%d/n", dwUid); PrintListData(ptList); } return LIST_OK; } static STATUS SetListData(TList **pptList) { STATUS nState; int nData = 0x12345678; char achBuf[10] = {0}; memcpy(&achBuf, &nData, 6); nState = PutChar(&(*pptList), 1, 'c'); if (LIST_OK != nState) { printf("Put Char to list fail[%d]!/n", nState); return nState; } nState = PutString(&(g_sptListTail->ptNext), 2, "string"); if (LIST_OK != nState) { printf("Put String to list fail[%d]!/n", nState); return nState; } nState = PutInteger(&(g_sptListTail->ptNext), 3, 3); if (LIST_OK != nState) { printf("Put Integer to list fail[%d]!/n", nState); return nState; } nState = PutBinary(&(g_sptListTail->ptNext), 4, (unsigned char *)&achBuf, 11); if (LIST_OK != nState) { printf("Put Binary to list fail[%d]!/n", nState); return nState; } return nState; } static STATUS PrtListData(TList **pptList) { STATUS nState; nState = PrintAllListData(&(*pptList), 1); nState = PrintAllListData(&(*pptList), 2); nState = PrintAllListData(&(*pptList), 3); nState = PrintAllListData(&(*pptList), 4); return nState; } int main(void) { STATUS nState; TList *ptList = NULL; unsigned int dwListNum; nState = SetListData(&ptList); if (LIST_OK != nState) { printf("Set list Data fail[%d]!/n", nState); return -1; } printf("Set list Data succ!/n"); dwListNum = ListGetNum(&ptList); printf("dwListNum:%d/n", dwListNum); nState = PrtListData(&ptList); if (LIST_OK != nState) { printf("Print list Data fail[%d]!/n", nState); return -1; } printf("Print list Data succ!/n"); nState = ListDestory(&ptList); if (LIST_OK != nState) { printf("Delete list Data fail[%d]!/n", nState); return -1; } printf("Delete list Data succ!/n"); return 0; }

你可能感兴趣的:(struct,list,String,测试,null,Integer)