#pragma once
typedef int DLDataType;
typedef struct DListNode {
struct DListNode* _pPre;
struct DListNode* _pNext;
DLDataType _data;
}DLNode,*PDLNode;
void DListInit(PDLNode* pHead);
void DListPushBack(PDLNode pHead, DLDataType data);
void DListPopBack(PDLNode pHead);
void DListPushFront(PDLNode pHead, DLDataType data);
void DListPopFront(PDLNode pHead);
void DListInsert(PDLNode pos, DLDataType data);
void DListErase(PDLNode pos);
void DListClear(PDLNode pHead);
void DListDestroy(PDLNode* pHead);
void DListPrint(PDLNode pHead);
PDLNode DListFind(PDLNode pHead, DLDataType data);
#include"DList.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
void DListInit(PDLNode* pHead) {
*pHead = (PDLNode)malloc(sizeof(DLNode));
if (*pHead == NULL) {
assert(0);
return;
}
(*pHead)->_pPre = *pHead;
(*pHead)->_pNext = *pHead;
}
PDLNode BuyDListNode(DLDataType data) {
PDLNode pNewNode = (PDLNode)malloc(sizeof(DLNode));
if (pNewNode == NULL) {
assert(0);
return NULL;
}
pNewNode->_pPre = NULL;
pNewNode->_pNext = NULL;
pNewNode->_data = data;
return pNewNode;
}
void DListPushBack(PDLNode pHead, DLDataType data) {
assert(pHead);
PDLNode pNewNode = BuyDListNode(data);
pNewNode->_pNext = pHead;
pNewNode->_pPre = pHead->_pPre;
pHead->_pPre->_pNext = pNewNode;
pHead->_pPre = pNewNode;
}
void DListPopBack(PDLNode pHead) {
assert(pHead);
if (pHead->_pNext == pHead) {
return;
}
PDLNode pDelNode = pHead->_pPre;
pDelNode->_pPre->_pNext = pHead;
pHead->_pPre = pDelNode->_pPre;
free(pDelNode);
}
void DListPushFront(PDLNode pHead, DLDataType data) {
assert(pHead);
PDLNode pNewNode = BuyDListNode(data);
pNewNode->_pPre = pHead;
pNewNode->_pNext = pHead->_pNext;
pHead->_pNext->_pPre = pNewNode;
pHead->_pNext = pNewNode;
}
void DListPopFront(PDLNode pHead) {
assert(pHead);
if (pHead->_pNext == pHead) {
return;
}
PDLNode pDelNode = pHead->_pNext;
pHead->_pNext = pDelNode->_pNext;
pDelNode->_pNext->_pPre = pHead;
free(pDelNode);
}
void DListInsert(PDLNode pos, DLDataType data) {
if (pos == NULL) {
assert(0);
return;
}
PDLNode pNewNode = BuyDListNode(data);
pNewNode->_pPre = pos;
pNewNode->_pNext = pos->_pNext;
pos->_pNext->_pPre = pNewNode;
pos->_pNext = pNewNode;
}
void DListErase(PDLNode pos) {
assert(pos);
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
free(pos);
}
void DListClear(PDLNode pHead) {
assert(pHead);
PDLNode pCur = pHead->_pNext;
while (pCur!=pHead) {
pHead->_pNext = pCur->_pNext;
free(pCur);
pCur = pHead->_pNext;
}
pHead->_pNext = pHead;
pHead->_pPre = pHead;
}
void DListDestroy(PDLNode* pHead) {
DListClear(*pHead);
free(*pHead);
*pHead = NULL;
}
void DListPrint(PDLNode pHead) {
assert(pHead);
PDLNode pCur = pHead->_pNext;
while (pCur!=pHead) {
printf("%d-->", pCur->_data);
pCur = pCur->_pNext;
}
printf("NUll\n");
}
PDLNode DListFind(PDLNode pHead, DLDataType data) {
assert(pHead);
PDLNode pCur = pHead->_pNext;
while (pCur!=pHead) {
if (pCur->_data == data) {
return pCur;
}
pCur = pCur->_pNext;
}
return NULL;
}