.h
# pragma once
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<assert.h>
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<assert.h>
typedef int DataType;
typedef struct DListNode
{
struct DListNode* pNext;
struct DListNode* pPre;
DataType _data;
}DLNode, *PDLNode;
void DListInit(PDLNode *pHead);
PDLNode BuyDListNode(DataType data);
void DListPushBack(PDLNode pHead, DataType data);
void DListPopBack(PDLNode pHead);
void DListPushFront(PDLNode pHead, DataType data);
void DListPopFront(PDLNode pHead);
void DListInsert(PDLNode pos, DataType data);
void PListErase(PDLNode pos);
int DListEmpty(PDLNode pHead);
int DListSize(PDLNode pHead);
void DListClear(PDLNode pHead);
void DListDestroy(PDLNode *pHead);
void PrintDList(PDLNode pHead);
PDLNode DListFind(PDLNode pHead, DataType data);
{
struct DListNode* pNext;
struct DListNode* pPre;
DataType _data;
}DLNode, *PDLNode;
void DListInit(PDLNode *pHead);
PDLNode BuyDListNode(DataType data);
void DListPushBack(PDLNode pHead, DataType data);
void DListPopBack(PDLNode pHead);
void DListPushFront(PDLNode pHead, DataType data);
void DListPopFront(PDLNode pHead);
void DListInsert(PDLNode pos, DataType data);
void PListErase(PDLNode pos);
int DListEmpty(PDLNode pHead);
int DListSize(PDLNode pHead);
void DListClear(PDLNode pHead);
void DListDestroy(PDLNode *pHead);
void PrintDList(PDLNode pHead);
PDLNode DListFind(PDLNode pHead, DataType data);
.c
# include"lianbiao3.h"
void DListInit(PDLNode *pHead)
{
assert(pHead);
*pHead = BuyDListNode(0);
}
PDLNode BuyDListNode(DataType data)
{
PDLNode pNewNode = (PDLNode)malloc(sizeof(DLNode));
if (NULL == pNewNode)
{
assert(pNewNode);
return NULL;
}
pNewNode->_data = data;
pNewNode->pNext = NULL;
pNewNode->pPre = NULL;
}
void DListPushBack(PDLNode pHead, DataType data)
{
PDLNode pCur = pHead;
PDLNode pNewNode = NULL;
assert(pHead);
while (pCur->pNext)
{
pCur = pCur->pNext;
}
pNewNode = BuyDListNode(data);
pCur->pNext = pNewNode;
pNewNode->pPre = pCur;
}
void DListPopBack(PDLNode pHead)
{
PDLNode pTailNode = pHead;
assert(pHead);
while (pTailNode->pNext)
pTailNode = pTailNode->pNext;
if (pTailNode != pHead)
{
pTailNode->pPre->pNext = NULL;
free(pTailNode);
}
}
void DListPushFront(PDLNode pHead, DataType data)
{
PDLNode pNewNode = NULL;
assert(pHead);
pNewNode = BuyDListNode(data);
pNewNode->pNext = pHead->pNext;
pHead->pNext = pNewNode;
pNewNode->pPre = pHead;
if (pNewNode->pNext)
pNewNode->pNext->pPre = pNewNode;
}
void DListPopFront(PDLNode pHead)
{
PDLNode pDelNode = NULL;
assert(pHead);
//空
if (NULL == pDelNode)
return;
//非空
pHead->pNext = pDelNode->pNext;
if (pDelNode->pNext)
pDelNode->pNext->pPre = pHead;
free(pDelNode);
}
void DListInsert(PDLNode pos, DataType data)
{
//new不能位于头节点
if (NULL == pos || NULL == pos->pPre)
return;
PDLNode pNewNode = NULL;
pNewNode = BuyDListNode(data);
pNewNode->pNext = pos;
pNewNode->pPre = pos->pPre;
pos->pPre = pNewNode;
pNewNode->pPre->pNext = pNewNode;
}
void PListErase(PDLNode pos)
{
//空位置||pos在头节点处删不了
if (NULL == pos || NULL == pos->pPre)
return;
pos->pPre->pNext = pos->pNext;
if (pos->pNext)
pos->pNext->pPre = pos->pPre;
}
int DListEmpty(PDLNode pHead)
{
assert(pHead);
return NULL == pHead->pNext;
}
int DListSize(PDLNode pHead)
{
//为有效节点的个数,不包含头节点
PDLNode pCur = NULL;
int count = 0;
assert(pHead);
pCur = pHead->pNext;
while (count)
{
count++;
pCur = pCur->pNext;
}
return count;
}
void DListClear(PDLNode pHead)
{
PDLNode pCur = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
pHead->pNext = pCur->pNext;
free(pCur);
pCur = pHead->pNext;
}
pHead->pNext;
}
void DListDestroy(PDLNode *pHead)
{
assert(pHead);
DListClear(*pHead);
free(*pHead);//删除头节点
*pHead = NULL;
}
void PrintDList(PDLNode pHead)
{
PDLNode pCur = NULL;
PDLNode pTailNode = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
printf("%d", pCur->_data);
pTailNode = pCur;
pCur = pCur->pNext;
}
printf("\n");
while (pTailNode != pCur)
{
printf("%d", pTailNode->_data);
pTailNode = pTailNode->pPre;
}
printf("\n");
}
PDLNode DListFind(PDLNode pHead, DataType data)
{
PDLNode pCur = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
if (pCur->_data == data)
return pCur;
pCur = pCur->pNext;
}
return NULL;
}
void DListInit(PDLNode *pHead)
{
assert(pHead);
*pHead = BuyDListNode(0);
}
PDLNode BuyDListNode(DataType data)
{
PDLNode pNewNode = (PDLNode)malloc(sizeof(DLNode));
if (NULL == pNewNode)
{
assert(pNewNode);
return NULL;
}
pNewNode->_data = data;
pNewNode->pNext = NULL;
pNewNode->pPre = NULL;
}
void DListPushBack(PDLNode pHead, DataType data)
{
PDLNode pCur = pHead;
PDLNode pNewNode = NULL;
assert(pHead);
while (pCur->pNext)
{
pCur = pCur->pNext;
}
pNewNode = BuyDListNode(data);
pCur->pNext = pNewNode;
pNewNode->pPre = pCur;
}
void DListPopBack(PDLNode pHead)
{
PDLNode pTailNode = pHead;
assert(pHead);
while (pTailNode->pNext)
pTailNode = pTailNode->pNext;
if (pTailNode != pHead)
{
pTailNode->pPre->pNext = NULL;
free(pTailNode);
}
}
void DListPushFront(PDLNode pHead, DataType data)
{
PDLNode pNewNode = NULL;
assert(pHead);
pNewNode = BuyDListNode(data);
pNewNode->pNext = pHead->pNext;
pHead->pNext = pNewNode;
pNewNode->pPre = pHead;
if (pNewNode->pNext)
pNewNode->pNext->pPre = pNewNode;
}
void DListPopFront(PDLNode pHead)
{
PDLNode pDelNode = NULL;
assert(pHead);
//空
if (NULL == pDelNode)
return;
//非空
pHead->pNext = pDelNode->pNext;
if (pDelNode->pNext)
pDelNode->pNext->pPre = pHead;
free(pDelNode);
}
void DListInsert(PDLNode pos, DataType data)
{
//new不能位于头节点
if (NULL == pos || NULL == pos->pPre)
return;
PDLNode pNewNode = NULL;
pNewNode = BuyDListNode(data);
pNewNode->pNext = pos;
pNewNode->pPre = pos->pPre;
pos->pPre = pNewNode;
pNewNode->pPre->pNext = pNewNode;
}
void PListErase(PDLNode pos)
{
//空位置||pos在头节点处删不了
if (NULL == pos || NULL == pos->pPre)
return;
pos->pPre->pNext = pos->pNext;
if (pos->pNext)
pos->pNext->pPre = pos->pPre;
}
int DListEmpty(PDLNode pHead)
{
assert(pHead);
return NULL == pHead->pNext;
}
int DListSize(PDLNode pHead)
{
//为有效节点的个数,不包含头节点
PDLNode pCur = NULL;
int count = 0;
assert(pHead);
pCur = pHead->pNext;
while (count)
{
count++;
pCur = pCur->pNext;
}
return count;
}
void DListClear(PDLNode pHead)
{
PDLNode pCur = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
pHead->pNext = pCur->pNext;
free(pCur);
pCur = pHead->pNext;
}
pHead->pNext;
}
void DListDestroy(PDLNode *pHead)
{
assert(pHead);
DListClear(*pHead);
free(*pHead);//删除头节点
*pHead = NULL;
}
void PrintDList(PDLNode pHead)
{
PDLNode pCur = NULL;
PDLNode pTailNode = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
printf("%d", pCur->_data);
pTailNode = pCur;
pCur = pCur->pNext;
}
printf("\n");
while (pTailNode != pCur)
{
printf("%d", pTailNode->_data);
pTailNode = pTailNode->pPre;
}
printf("\n");
}
PDLNode DListFind(PDLNode pHead, DataType data)
{
PDLNode pCur = NULL;
assert(pHead);
pCur = pHead->pNext;
while (pCur)
{
if (pCur->_data == data)
return pCur;
pCur = pCur->pNext;
}
return NULL;
}