#ifndef SHLIST_H #define SHLIST_H struct SHListNode { char* pstrText; // 保存传入的部分文档 int nLen ; // Node长度 SHListNode* pNext; // 下一块传入的文档 SHListNode() { pNext = NULL; nLen = 0; pstrText = NULL; } }; struct SHList { int nLen ; // 文档长度 SHListNode* pHead; // 下一块传入的文档 SHListNode* pTail; // 保存链表尾部 SHList() { pTail = NULL; pHead = NULL; nLen = 0; } }; void AppendToList(SHList& textList, char* pstrText,int nLen); void DeleteAllElement(SHList& textList); char* MergeTextInList(SHList& textList); int GetListLen(SHList& textList); #endif; #include "stdafx.h" #include "SHList.h" #include <stdlib.h> #include <assert.h> struct SHListNode; struct SHList; void AppendToList(SHList& textList, char* pstrText,int nLen) { SHListNode* pTempCell = (SHListNode*)malloc(sizeof(SHListNode));; assert(NULL != pTempCell); pTempCell->pstrText = (char*)malloc(nLen + 1); assert(NULL != pTempCell->pstrText); pTempCell->pstrText[nLen] = '/0'; memcpy(pTempCell->pstrText,pstrText,nLen); pTempCell->pNext = NULL ; pTempCell->nLen = nLen ; if(NULL != textList.pTail) textList.pTail->pNext = pTempCell ; textList.pTail = pTempCell ; if(NULL == textList.pHead) textList.pHead = pTempCell ; textList.nLen += nLen ; } void DeleteAllElement(SHList& textList) { SHListNode* pTempCell = textList.pHead ; while(NULL != pTempCell) { SHListNode* pDelCell = pTempCell ; pTempCell = pTempCell->pNext ; assert(NULL != pDelCell->pstrText); free(pDelCell->pstrText) ; free(pDelCell) ; } textList.pHead = NULL ; textList.pTail = NULL ; textList.nLen = 0; } char* MergeTextInList(SHList& textList) { char* pstrText = NULL ; if(textList.nLen > 0) { pstrText = (char*)malloc(textList.nLen + 1); pstrText[textList.nLen] = '/0'; char* pstrTemp = pstrText ; SHListNode* pTempCell = textList.pHead ; while(NULL != pTempCell) { memcpy(pstrTemp,pTempCell->pstrText,pTempCell->nLen); pstrTemp += pTempCell->nLen ; pTempCell = pTempCell->pNext ; } //assert(pstrText + textList.nLen == pstrTemp); } DeleteAllElement(textList); return pstrText ; } int GetListLen(SHList& textList) { return textList.nLen; }