带头结点双向循环链表的实现
DList.h
# pragma once
# include <stdlib.h>
# include <stdio.h>
# include <assert.h>
typedef int DLDataType;
typedef struct DListNode
{
struct DListNode * _pNext;
struct DListNode * _pPre;
DLDataType data;
} DLNode;
void DListInit ( DLNode* * pHead) ;
void DListPushBack ( DLNode* pHead, DLDataType data) ;
void DListPopBack ( DLNode* pHead) ;
void DListPushFront ( DLNode* pHead, DLDataType data) ;
void DListPopFront ( DLNode* pHead) ;
void DListDestroy ( DLNode* * pHead) ;
DLNode* BuyDLNode ( DLDataType data) ;
void PrintList ( DLNode* pHead) ;
DLNode* DListFind ( DLNode* pHead, DLDataType data) ;
void DListInsert ( DLNode* pos, DLDataType data) ;
void DListErase ( DLNode* pos) ;
void DListClear ( DLNode* pos) ;
DList.c
# include "DList.h"
DLNode* BuyDLNode ( DLDataType data)
{
DLNode* pNewNode = ( DLNode* ) malloc ( sizeof ( DLNode) ) ;
if ( NULL == pNewNode)
{
assert ( 0 ) ;
return NULL ;
}
pNewNode-> _pNext = NULL ;
pNewNode-> _pPre = NULL ;
pNewNode-> data = data;
return pNewNode;
}
void DListInit ( DLNode* * pHead)
{
* pHead = BuyDLNode ( 0 ) ;
( * pHead) -> _pNext = * pHead;
( * pHead) -> _pPre = * pHead;
}
void DListPushBack ( DLNode* pHead, DLDataType data)
{
DLNode* pNewNode = NULL ;
assert ( pHead) ;
pNewNode = BuyDLNode ( data) ;
pNewNode-> _pNext = pHead;
pNewNode-> _pPre = pHead-> _pPre;
pHead-> _pPre = pNewNode;
pNewNode-> _pPre-> _pNext = pNewNode;
}
void PrintList ( DLNode* pHead)
{
DLNode* pCur = NULL ;
assert ( pHead) ;
pCur = pHead-> _pNext;
while ( pCur != pHead)
{
printf ( "%d " , pCur-> data) ;
pCur = pCur-> _pNext;
}
printf ( "\n" ) ;
}
void DListPopBack ( DLNode* pHead)
{
DLNode* pDelNode = NULL ;
assert ( pHead) ;
if ( NULL == pHead-> _pNext)
return ;
pDelNode = pHead-> _pPre;
pDelNode-> _pPre-> _pNext = pHead;
pHead-> _pPre = pDelNode-> _pPre;
free ( pDelNode) ;
}
void DListPushFront ( DLNode* pHead, DLDataType data)
{
assert ( pHead) ;
DLNode* pNewNode = BuyDLNode ( data) ;
pNewNode-> _pNext = pHead-> _pNext;
pNewNode-> _pPre = pHead;
pHead-> _pNext = pNewNode;
pNewNode-> _pNext-> _pPre = pNewNode;
}
void DListPopFront ( DLNode* pHead)
{
DLNode* pDelNode = NULL ;
assert ( pHead) ;
if ( pHead-> _pNext == pHead)
return ;
pDelNode = pHead-> _pNext;
pHead-> _pNext = pDelNode-> _pNext;
pDelNode-> _pNext-> _pPre = pHead;
free ( pDelNode) ;
}
DLNode* DListFind ( DLNode* pHead, DLDataType data)
{
DLNode* pCur = NULL ;
assert ( pHead) ;
pCur = pHead-> _pNext;
while ( pCur != pHead)
{
if ( pCur-> data == data)
return pCur;
pCur = pCur-> _pNext;
}
return NULL ;
}
void DListInsert ( DLNode* pos, DLDataType data)
{
DLNode* pDelNode = NULL ;
if ( NULL == pos)
return ;
DLNode* pNewNode = BuyDLNode ( data) ;
pNewNode-> _pNext = pos;
pNewNode-> _pPre = pos-> _pPre;
pos-> _pPre = pNewNode;
pNewNode-> _pPre-> _pNext = pNewNode;
}
void DListErase ( DLNode* pos)
{
if ( NULL == pos)
return ;
pos-> _pPre-> _pNext = pos-> _pNext;
pos-> _pNext-> _pPre = pos-> _pPre;
free ( pos) ;
}
void DListDestroy ( DLNode* * pHead)
{
DListClear ( * pHead) ;
free ( * pHead) ;
* pHead = NULL ;
}
void DListClear ( DLNode* pHead)
{
DLNode* pCur = NULL ;
assert ( pHead) ;
pCur = pHead-> _pNext;
while ( pCur != pHead)
{
pHead-> _pNext = pCur-> _pNext;
free ( pCur) ;
pCur = pHead-> _pNext;
}
pHead-> _pNext = pHead;
pHead-> _pPre = pHead;
}
main.c
# include "DList.h"
int main ( )
{
DLNode* pHead = NULL ;
DListInit ( & pHead) ;
DListPushBack ( pHead, 1 ) ;
DListPushBack ( pHead, 2 ) ;
DListPushBack ( pHead, 3 ) ;
DListPushBack ( pHead, 4 ) ;
DListPushBack ( pHead, 5 ) ;
PrintList ( pHead) ;
DListPopBack ( pHead) ;
DListPopBack ( pHead) ;
PrintList ( pHead) ;
DListPushFront ( pHead, 10 ) ;
DListPushFront ( pHead, 20 ) ;
DListPushFront ( pHead, 30 ) ;
DListPushFront ( pHead, 40 ) ;
DListPushFront ( pHead, 50 ) ;
PrintList ( pHead) ;
DListPopFront ( pHead) ;
DListPopFront ( pHead) ;
PrintList ( pHead) ;
DListDestroy ( & pHead) ;
}