带头结点的双向循环链表
程序代码如下:
Dlist.h
//Dlist.h
typedef int DataType;
typedef struct DListNode
{
struct DListNode* _pNext;
struct DListNode* _pPre;
DataType _data;
}DListNode;
// 初始化
void DListInit(DListNode** pHead);
// 双向链表的尾插
void DListPushBack(DListNode* pHead, DataType data);
// 双向链表的尾删
void DListPopBack(DListNode* pHead);
// 双向链表的头插
void DListPushFront(DListNode* pHead, DataType data);
// 双向链表的头删
void DListPopFront(DListNode* pHead);
// 任意位置插入
void DListInsert(DListNode* pos, DataType data);
// 任意位置删除
void DListErase(DListNode* pos);
// 查找值为data的结点
DListNode* DListFind(DListNode* pHead, DataType data);
// 销毁
void DListDestroy(DListNode** pHead);
//打印
void PrintDlist(DListNode* pHead);
Dlist.c
//Dlist.c
#include <stdio.h>
#include <assert.h>
#include "Dlist.h"
// 初始化
void DListInit(DListNode** pHead)
{
assert(pHead);
(*pHead) = (DListNode*)malloc(sizeof(DListNode));
(*pHead)->_pNext = (*pHead);
(*pHead)->_pPre = (*pHead);
(*pHead)->_data = 0;
}
DListNode* BuyNode(DataType d)
{
DListNode* newNode = (DListNode*)malloc(sizeof(DListNode));
if (NULL == newNode)
{
assert(0);
return NULL;
}
newNode->_data = d;
newNode->_pNext = NULL;
newNode->_pPre = NULL;
return newNode;
}
// 双向链表的尾插
void DListPushBack(DListNode* pHead, DataType data)
{
DListNode* newNode = BuyNode(data);
DListNode* tail = pHead;
while (pHead != tail->_pNext)
{
tail = tail->_pNext;
}
tail->_pNext = newNode;
newNode->_pNext = pHead;
pHead->_pPre = newNode;
newNode->_pPre = tail;
}
// 双向链表的尾删
void DListPopBack(DListNode* pHead)
{
DListNode* tail = pHead;
if (pHead == pHead->_pNext)
return;
while (pHead != tail->_pNext)
{
tail = tail->_pNext;
}
tail->_pNext = NULL;
tail->_pPre->_pNext = pHead;
tail->_pPre = NULL;
free(tail);
tail = NULL;
}
// 双向链表的头插
void DListPushFront(DListNode* pHead, DataType data)
{
DListNode* newNode = BuyNode(data);
pHead->_pNext->_pPre = newNode;
newNode->_pNext = pHead->_pNext;
pHead->_pNext = newNode;
newNode->_pPre = pHead;
}
// 双向链表的头删
void DListPopFront(DListNode* pHead)
{
DListNode* temp = NULL;
if (pHead == pHead->_pNext)
return;
temp = pHead->_pNext;
pHead->_pNext->_pPre = NULL;
pHead->_pNext = pHead->_pNext->_pNext;
pHead->_pNext->_pPre->_pNext = NULL;
pHead->_pNext->_pPre = pHead;
free(temp);
temp = NULL;
}
// 任意位置插入
void DListInsert(DListNode* pos, DataType data)
{
if (pos == pos->_pNext)
{
DListPushFront(pos, data);
return;
}
DListNode* newNode = BuyNode(data);
pos->_pPre->_pNext = newNode;
newNode->_pPre = pos->_pPre;
newNode->_pNext = pos;
pos->_pPre = newNode;
}
// 任意位置删除
void DListErase(DListNode* pos)
{
if (pos == pos->_pNext)
return;
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
pos->_pPre = NULL;
pos->_pNext = NULL;
free(pos);
pos == NULL;
}
// 查找值为data的结点
DListNode* DListFind(DListNode* pHead, DataType data)
{
DListNode* cur = pHead;
while (pHead != cur->_pNext)
{
if (data == cur->_data)
return cur;
cur = cur->_pNext;
}
return NULL;
}
// 销毁
void DListDestroy(DListNode** pHead)
{
assert(pHead);
DListNode* cur1 = (*pHead)->_pNext;
DListNode* cur2 = NULL;
while ((*pHead) != cur1)
{
cur2 = cur1->_pNext;
free(cur1);
cur1 = cur2;
}
free((*pHead));
(*pHead) = NULL;
}
//打印
void PrintDlist(DListNode* pHead)
{
DListNode* cur = pHead;
if (NULL == pHead)
{
printf("NULL\n");
return;
}
while (pHead != cur->_pNext)
{
printf("%d-->", cur->_data);
cur = cur->_pNext;
}
printf("%d-->%d\n", cur->_data,pHead->_data);
}
test.c
//test.c
#include <stdio.h>
#include <stdlib.h>
#include "Dlist.h"
Test()
{
DListNode* pHead;
// 初始化
DListInit(&pHead);
PrintDlist(pHead);
// 双向链表的尾插
DListPushBack(pHead, 6);
DListPushBack(pHead, 7);
DListPushBack(pHead, 8);
DListPushBack(pHead, 9);
DListPushBack(pHead, 10);
PrintDlist(pHead);
// 双向链表的尾删
DListPopBack(pHead);
PrintDlist(pHead);
// 双向链表的头插
DListPushFront(pHead, 4);
DListPushFront(pHead, 3);
DListPushFront(pHead, 2);
DListPushFront(pHead, 1);
DListPushFront(pHead, 1);
PrintDlist(pHead);
// 双向链表的头删
DListPopFront(pHead);
PrintDlist(pHead);
// 查找值为6的结点
DListNode* pos = DListFind(pHead, 6);
// 任意位置插入
DListInsert(pos, 5);
PrintDlist(pHead);
// 查找值为7的结点
pos = DListFind(pHead, 7);
// 任意位置删除
DListErase(pos);
PrintDlist(pHead);
// 销毁
DListDestroy(&pHead);
PrintDlist(pHead);
}
int main()
{
Test();
system("pause");
return 0;
}
程序运行结果如下: