头文件
pragma once
#include <malloc.h>
#include <assert.h>
#include <stdio.h>
typedef int DataType;
typedef struct DCLNode
{
struct DCLNode* _pNext;
struct DCLNode* _pPre;
DataType _data;
}Node, *PNode;
void DCLInit(PNode* ppHead);
void DCLPushBack(PNode pHead, DataType data);
void DCLPopBack(PNode pHead);
void DCLPushFront(PNode pHead, DataType data);
void DCLPopFront(PNode pHead);
void DCLInsert(PNode pos, DataType data);
void DCLErase(PNode pos);
void DCLDestroy(PNode* ppHead);
PNode DCLFind(PNode pHead, DataType data);
PNode DCLBuyNode(DataType data);
void DCLPrintList(PNode pHead);
函数实现
#include"shuang.h"
typedef struct DCLNode
{
struct DCLNode* _pNext;
struct DCLNode* _pPre;
DataType _data;
}Node, *PNode;
PNode DCLBuyNode(DataType data)
{
PNode pnewnode = (DataType *)malloc(sizeof(DataType));
if (pnewnode==NULL)
{
return;
}
pnewnode->_pNext = NULL;
pnewnode->_pPre = NULL;
pnewnode->_data = data;
}
void DCLInit(PNode* ppHead)
{
assert(ppHead);
*ppHead = DCLBuyNode(0);
(*ppHead)->_pPre == NULL;
(*ppHead)->_pNext = NULL;
}
void DCLPushBack(PNode pHead, DataType data)
{
assert(pHead);
PNode pcur = NULL;
PNode newnode = DCLBuyNode(data);
pcur = pHead->_pPre;
pcur->_pNext = newnode->_pPre;
newnode->_pPre = pcur;
newnode->_pNext = pHead;
pHead->_pPre = newnode;
}
void DCLPopBack(PNode pHead)
{
assert(pHead);
if (pHead->_pNext == pHead)
{
return;
}
PNode pdel = pHead->_pPre->_pPre;
free(pdel);
pdel->_pNext = pHead;
pHead->_pPre = pdel;
}
void DCLPushFront(PNode pHead, DataType data)
{
assert(pHead);
PNode newnode = DCLBuyNode(data);
newnode->_pNext = pHead->_pPre;
pHead->_pPre = newnode->_pNext;
pHead->_pPre->_pNext = newnode;
newnode->_pPre = pHead->_pPre;
}
void DCLPopFront(PNode pHead)
{
assert(pHead);
if (pHead->_pNext == pHead)
{
return;
}
else
{
PNode pDel = pHead->_pNext;
pHead->_pNext = pDel->_pNext;
pDel->_pNext->_pPre = pHead;
free(pDel);
}
}
void DCLInsert(PNode pos, DataType data)
{
PNode newnode = DCLBuyNode(data);
newnode->_pNext = pos;
pos->_pPre->_pNext = newnode;
pos->_pPre = newnode;
newnode->_pPre = pos->_pPre;
}
void DCLErase(PNode pos)
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
void DCLDestroy(PNode* ppHead)
{
assert(ppHead);
PNode pdel = NULL;
PNode pcur = *ppHead;
while (pcur)
{
pdel = pcur;
pcur = pcur->_pNext;
free(pdel);
}
free(pcur);
*ppHead = NULL;
}
PNode DCLFind(PNode pHead, DataType data)
{
assert(pHead);
PNode pcur = pHead;
while (pcur)
{
if (pcur->_data == data)
{
return pcur;
}
pcur = pcur->_pNext;
}
return 0;
}
void DCLPrintList(PNode pHead)
{
assert(pHead);
PNode pcur = pHead;
while (pcur)
{
printf("%d ", pcur->_data);
pcur = pcur->_pNext;
}
}