LinkedList 物件
只是單純的一個LinkedList物件.......
比較特別的部分應該是反轉功能(Reverse)
class CLinkedList
{
public:
CLinkedList();
~CLinkedList();
void Insert(int iSequence_, char* pData_);
void Remove(int iSequence_);
void GetData(int iSequence_, char* pData_, size_t Size_);
void Reverse();
int Count();
private:
typedef struct _tagNode_
{
char acData[256];
_tagNode_* pNextNode;
_tagNode_* pPrevNode;
}tagNode;
tagNode* _pHeadNode;
int _iCount;
};
CLinkedList::CLinkedList()
{
_pHeadNode = new tagNode();
_pHeadNode->pPrevNode = NULL;
_pHeadNode->pNextNode = NULL;
::strcpy(_pHeadNode->acData, "_HEAD_");
_iCount = 0;
}
CLinkedList::~CLinkedList()
{
tagNode* pNextNode = _pHeadNode;
tagNode* pCurrNode = NULL;
do
{
pCurrNode = pNextNode;
pNextNode = pNextNode->pNextNode;
//::printf("delete %s\n", pCurrNode->acData);
delete pCurrNode;
}while(pNextNode!=NULL);
}
void CLinkedList::Reverse()
{
tagNode* pCurr = _pHeadNode->pNextNode;
tagNode* pNext = NULL;
for(int i=0; i<_iCount; i++)
{
pNext = pCurr->pNextNode;
if(i==0)
pCurr->pPrevNode = NULL;
if(i==_iCount-1)
{
pCurr->pNextNode = _pHeadNode;
_pHeadNode->pNextNode = pCurr;
}
tagNode* pTmp = pCurr->pPrevNode;
pCurr->pPrevNode = pCurr->pNextNode;
pCurr->pNextNode = pTmp;
pCurr = pNext;
}
}
void CLinkedList::Remove(int iSequence_)
{
tagNode* pCurr = _pHeadNode;
for(int i=0; i<=iSequence_; i++)
pCurr = pCurr->pNextNode;
tagNode* pPrev = pCurr->pPrevNode;
tagNode* pNext = pCurr->pNextNode;
pPrev->pNextNode = pNext;
pNext->pPrevNode = pPrev;
delete pCurr;
_iCount--;
}
void CLinkedList::Insert(int iSequence_, char* pData_)
{
tagNode* pCurrNode = new tagNode();
::memcpy(pCurrNode->acData, pData_, sizeof(pCurrNode->acData));
tagNode* pPrev = _pHeadNode;
for(int i=0; i<iSequence_; i++)
pPrev = pPrev->pNextNode;
tagNode* pNext = pPrev->pNextNode;
if(pNext!=NULL)
pNext->pPrevNode = pCurrNode;
pPrev->pNextNode = pCurrNode;
pCurrNode->pPrevNode = pPrev;
pCurrNode->pNextNode = pNext;
_iCount++;
}
void CLinkedList::GetData(int iSequence_, char* pData_, size_t Size_)
{
tagNode* pCurr = _pHeadNode;
for(int i=0; i<=iSequence_; i++)
pCurr = pCurr->pNextNode;
::memcpy(pData_, pCurr->acData, Size_);
}
int CLinkedList::Count()
{
return _iCount;
}