给出双向循环链表的定义:
typedef int DataType;
typedef struct DCLNode
{
struct DCLNode* _pNext;
struct DCLNode* _pPre;
DataType _data;
}Node, *PNode;
将函数的声明放在head.h的头文件里面:
#include "DCList.h"
PNode DCLBuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
assert(0);
}
pNewNode->_data = data;
pNewNode->_pNext = NULL;
pNewNode->_pPre = NULL;
return pNewNode;
}
void DCLInit(PNode* ppHead)
{
assert(ppHead);
*ppHead = DCLBuyNode(0);
if (NULL == *ppHead)
{
assert(0);
}
(*ppHead)->_pNext = *ppHead;
(*ppHead)->_pPre = *ppHead;
}
void DCLPushBack(PNode pHead, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
PNode pTailNode = NULL;
assert(pHead);
pTailNode = pHead->_pPre; //找到最后一个结点
pTailNode->_pNext = pNewNode;
pNewNode->_pPre = pTailNode;
pNewNode->_pNext = pHead;
pHead->_pPre = pNewNode;
}
void DCLPopBack(PNode pHead)
{
if (pHead->_pNext == pHead)
{
return;
}
else
{
PNode pPreTail = pHead->_pPre->_pPre;
free(pPreTail->_pNext);
pPreTail->_pNext = pHead;
pHead->_pPre = pPreTail;
}
}
void DCLPushFront(PNode pHead, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
assert(pHead);
pNewNode->_pNext = pHead->_pNext;
pHead->_pNext->_pPre = pNewNode;
pNewNode->_pPre = pHead;
pHead->_pNext = pNewNode;
}
void DCLPopFront(PNode pHead)
{
if (pHead->_pNext == pHead)
{
return;
}
else
{
PNode pDel = pHead->_pNext;
pHead->_pNext = pDel->_pNext;
pDel->_pNext->_pPre = pHead;
free(pDel);
}
}
void DCLPrintList(PNode pHead)
{
PNode pCur = NULL;
assert(pHead);
pCur = pHead->_pNext;
while (pCur != pHead)
{
printf("%d--->", pCur->_data);
pCur = pCur->_pNext;
}
printf("\n");
}
void DCLDestroy(PNode* ppHead)
{
PNode pCur = NULL;
assert(ppHead);
pCur = (*ppHead)->_pNext;
while (pCur != *ppHead)
{
pCur = pCur->_pNext;
free(pCur->_pPre);
}
free(pCur->_pPre);
free(pCur);
*ppHead = NULL;
}
void DCLDestroy_P(PNode* ppHead)
{
PNode pCur = NULL;
PNode pPre = NULL;
assert(ppHead);
pCur = (*ppHead)->_pNext;
while (pCur != *ppHead)
{
pPre = pCur;
pCur = pCur->_pNext;
free(pPre);
}
free(pCur);
*ppHead = NULL;
}
void DCLInsert(PNode pos, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
assert(pNewNode);
pNewNode->_pNext = pos;
pos->_pPre->_pNext = pNewNode;
pNewNode->_pPre = pos->_pPre;
pos->_pPre = pNewNode;
}
void DCLErase(PNode pos)
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
PNode DCLFind(PNode pHead, DataType data)
{
PNode pCur = pHead->_pNext;
while (pCur != pHead)
{
if (pCur->_data == data)
{
return pCur;
}
pCur = pCur->_pNext;
}
return NULL;
}
函数的定义(具体实现):
#include "DCList.h"
PNode DCLBuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
assert(0);
}
pNewNode->_data = data;
pNewNode->_pNext = NULL;
pNewNode->_pPre = NULL;
return pNewNode;
}
void DCLInit(PNode* ppHead)
{
assert(ppHead);
*ppHead = DCLBuyNode(0);
if (NULL == *ppHead)
{
assert(0);
}
(*ppHead)->_pNext = *ppHead;
(*ppHead)->_pPre = *ppHead;
}
void DCLPushBack(PNode pHead, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
PNode pTailNode = NULL;
assert(pHead);
pTailNode = pHead->_pPre; //找到最后一个结点
pTailNode->_pNext = pNewNode;
pNewNode->_pPre = pTailNode;
pNewNode->_pNext = pHead;
pHead->_pPre = pNewNode;
}
void DCLPopBack(PNode pHead)
{
if (pHead->_pNext == pHead)
{
return;
}
else
{
PNode pPreTail = pHead->_pPre->_pPre;
free(pPreTail->_pNext);
pPreTail->_pNext = pHead;
pHead->_pPre = pPreTail;
}
}
void DCLPushFront(PNode pHead, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
assert(pHead);
pNewNode->_pNext = pHead->_pNext;
pHead->_pNext->_pPre = pNewNode;
pNewNode->_pPre = pHead;
pHead->_pNext = pNewNode;
}
void DCLPopFront(PNode pHead)
{
if (pHead->_pNext == pHead)
{
return;
}
else
{
PNode pDel = pHead->_pNext;
pHead->_pNext = pDel->_pNext;
pDel->_pNext->_pPre = pHead;
free(pDel);
}
}
void DCLPrintList(PNode pHead)
{
PNode pCur = NULL;
assert(pHead);
pCur = pHead->_pNext;
while (pCur != pHead)
{
printf("%d--->", pCur->_data);
pCur = pCur->_pNext;
}
printf("\n");
}
void DCLDestroy(PNode* ppHead)
{
PNode pCur = NULL;
assert(ppHead);
pCur = (*ppHead)->_pNext;
while (pCur != *ppHead)
{
pCur = pCur->_pNext;
free(pCur->_pPre);
}
free(pCur->_pPre);
free(pCur);
*ppHead = NULL;
}
void DCLDestroy_P(PNode* ppHead)
{
PNode pCur = NULL;
PNode pPre = NULL;
assert(ppHead);
pCur = (*ppHead)->_pNext;
while (pCur != *ppHead)
{
pPre = pCur;
pCur = pCur->_pNext;
free(pPre);
}
free(pCur);
*ppHead = NULL;
}
void DCLInsert(PNode pos, DataType data)
{
PNode pNewNode = DCLBuyNode(data);
assert(pNewNode);
pNewNode->_pNext = pos;
pos->_pPre->_pNext = pNewNode;
pNewNode->_pPre = pos->_pPre;
pos->_pPre = pNewNode;
}
void DCLErase(PNode pos)
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
PNode DCLFind(PNode pHead, DataType data)
{
PNode pCur = pHead->_pNext;
while (pCur != pHead)
{
if (pCur->_data == data)
{
return pCur;
}
pCur = pCur->_pNext;
}
return NULL;
}
测试代码:
#include "DCList.h"
void TestDCList()
{
PNode pHead, pos;
DCLInit(&pHead);
DCLPushBack(pHead, 1);
DCLPushBack(pHead, 2);
DCLPushBack(pHead, 3);
DCLPushBack(pHead, 4);
DCLPrintList(pHead);
DCLPopBack(pHead);
DCLPrintList(pHead);
DCLPushFront(pHead, 0);
DCLPrintList(pHead);
DCLPopFront(pHead);
DCLPrintList(pHead);
pos = DCLFind(pHead, 2);
DCLInsert(pos, 5);
DCLPrintList(pHead);
pos = DCLFind(pHead, 5);
DCLErase(pos);
DCLPrintList(pHead);
DCLDestroy(&pHead);
}
int main()
{
TestDCList();
system("pause");
return 0;
}