DList.h
#pragma once
#include<malloc.h>
#include<assert.h>
typedef int DateType;
typedef struct DListNode
{
struct DListNode* pNext;
struct DListNode* pPre;
DateType data;
}DLNode, *PDNode;
void DListInit(PDNode* pHead);
void DListPushBack(PDNode pHead, DateType data);
void DListPopBack(PDNode pHead);
void DListPushFront(PDNode pHead, DateType data);
void DListPopFront(PDNode pHead);
void DListInsert(PDNode pos, DateType data);
void DListErase(PDNode pos);
int DListEmpty(PDNode pHead);
int DListSize(PDNode pHead);
void DListClear(PDNode pHead);
void DListDestory(PDNode* pHead);
PDNode BuyDListNode(DateType data);
DList.c
#include "DList.h"
void DListInit(PDNode * pHead)
{
assert(pHead);
*pHead = BuyDListNode(0);
}
void DListPushBack(PDNode pHead, DateType data)
{
PDNode pCur = pHead;
PDNode pNewNode = NULL;
assert(pHead);
while (pCur->pNext)
{
pCur = pCur->pNext;
}
pNewNode = BuyDListNode(data);
pCur->pNext = pNewNode;
pNewNode->pPre = pCur;
}
void DListPopBack(PDNode pHead)
{
PDNode pTailNode = pHead;
assert(pHead);
while (pTailNode->pNext)
{
pTailNode = pTailNode->pNext;
}
if (pTailNode != pHead)
{
pTailNode->pPre->pNext = NULL;
free(pTailNode);
}
}
void DListPushFront(PDNode pHead, DateType data)
{
PDNode pNewNode = NULL;
assert(pHead);
pNewNode = BuyDListNode(data);
pNewNode->pNext = pHead->pNext;
pHead->pNext = pNewNode;
pNewNode->pPre = pHead;
if (pNewNode->pNext != NULL)
{
pNewNode->pNext->pPre = pNewNode;
}
}
void DListPopFront(PDNode pHead)
{
PDNode pDelNode = NULL;
assert(pHead);
pDelNode = pHead->pNext;
if (pDelNode == NULL)
{
return;
}
pHead->pNext = pDelNode->pNext;
if (pDelNode->pNext)
{
pDelNode->pNext->pPre = pHead;
}
free(pDelNode);
}
void DListInsert(PDNode pos, DateType data)
{
PDNode pNewNode = NULL;
if (pos == NULL || pos->pPre == NULL)
{
return;
}
pNewNode = BuyDListNode(data);
pNewNode->pNext = pos;
pNewNode->pPre = pos->pPre;
pos->pPre = pNewNode;
pNewNode->pPre->pNext = pNewNode;
}
void DListErase(PDNode pos)
{
if (pos == NULL || pos->pPre == NULL)
{
return;
}
pos->pPre->pNext = pos->pNext;
if (pos->pNext)
{
pos->pNext->pPre = pos->pPre;
}
}
int DListEmpty(PDNode pHead)
{
assert(pHead);
return NULL == pHead->pNext;
}
int DListSize(PDNode pHead)
{
PDNode pCur = NULL;
int count = 0;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
count++;
pCur = pCur->pNext;
}
return count;
}
void DListClear(PDNode pHead)
{
PDNode pCur = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
pHead->pNext = pCur->pNext;
free(pCur);
pCur = pHead->pNext;
}
}
void DListDestory(PDNode * pHead)
{
assert(pHead);
DListClear(*pHead);
free(*pHead);
*pHead = NULL;
}
PDNode BuyDListNode(DateType data)
{
PDNode pNewNode = (PDNode)malloc(sizeof(DLNode));
if (pNewNode == NULL)
{
assert(0);
return NULL;
}
pNewNode->data = data;
pNewNode->pNext = NULL;
pNewNode->pPre = NULL;
return pNewNode;
}