代码示例:
Dlist.h
#pragma once
typedef int Datatype;
typedef struct DlistNode
{
Datatype data;
struct DlistNode* _next;
struct DlistNode* _prev;
}Node, *pNode;
pNode _head;
void DlistInit(pNode *_head);
pNode BuyDListNode(Datatype data);
void DlistPushfront(pNode _head, Datatype data);
void DlistPopfront(pNode _head);
void DlistPushback(pNode _head, Datatype data);
void DlistPopback(pNode _head);
int DlistFind(pNode _head);
pNode DlistFindpos(pNode _head, int data);
void DListInsert(pNode pos, Datatype data);
void DListErase(pNode pos);
void DListClear(pNode pHead);
void DListDestroy(pNode* _head);
void DlistPrint(pNode _head);
Test.c
#include "Dlist.h"
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
//链表初始化
void DlistInit(pNode *_head)
{
assert(_head);
*_head = (pNode)malloc(sizeof(Node));
if (*_head == NULL)
{
assert(0);
return ;
}
(*_head)->_next = *_head;
(*_head)->_prev = *_head;
}
//创建新节点
pNode BuyDListNode(Datatype data)
{
pNode pnewNode = (pNode)malloc(sizeof(Node));
if (pnewNode == NULL)
{
return 0;
}
pnewNode->_next = NULL;
pnewNode->_prev = NULL;
pnewNode->data = data;
return pnewNode;
}
//头插
void DlistPushfront(pNode _head,Datatype data)
{
assert(_head);
pNode node = BuyDListNode(data);
node->_prev = _head;
node->_next = _head->_next;
_head->_next = node;
_head->_next->_prev = node;
}
//头删
void DlistPopfront(pNode _head)
{
assert(_head);
if (_head->_next == _head)
{
return;
}
pNode delnode = _head->_next;
delnode->_next->_prev = _head;
_head->_next = delnode->_next;
free(delnode);
}
//尾插
void DlistPushback(pNode _head,Datatype data)
{
assert(_head);
pNode node = NULL;
node=BuyDListNode(data);
node->_prev = _head->_prev;
node->_next = _head;
_head->_prev->_next = node;
_head->_prev = node;
}
//尾删
void DlistPopback(pNode _head)
{
assert(_head);
if (_head->_next == _head)
{
return;
}
pNode delnode = _head->_prev;
delnode->_prev->_next = _head;
_head->_prev = delnode->_prev;
free(delnode);
}
//查找链表有效节点的个数
int DlistFind(pNode _head)
{
assert(_head);
pNode pCur = _head;
int count = 0;
while (pCur->_next != _head)
{
++count;
pCur = pCur->_next;
}
return count;
}
//查找节点的地址
pNode DlistFindpos(pNode _head,int data)
{
assert(_head);
pNode pCur = _head;
int count = 0;
while (pCur->_next != _head)
{
if (count == data)
{
return pCur;
}
++count;
pCur = pCur->_next;
}
return NULL;
}
//任意位置插入 !!! (使用头插之后使用有点问题,还没解决)
void DListInsert(pNode pos, Datatype data)
{
assert(pos);
pNode node = BuyDListNode(data);
node->_next = pos;
node->_prev = pos->_prev;
pos->_prev = node;
node->_prev->_next = node;
}
//任意位置清除节点
void DListErase(pNode pos)
{
assert(pos);
pos->_next->_prev = pos->_prev;
pos->_prev->_next = pos->_next;
free(pos);
}
//清空链表
void DListClear(pNode pHead)
{
assert(_head);
pNode pCur = _head->_next;
while (pCur != _head)
{
_head->_next = pCur->_next;
pCur->_next->_prev = _head;
free(pCur);
pCur = _head->_next;
if (pCur == _head)
{
_head->_next = _head;
_head->_prev = _head;
}
}
}
//销毁链表 !!! (这里传二级指针)
void DListDestroy(pNode* _head)
{
DListClear(*_head);
free(*_head);
*_head = NULL;
}
//打印链表
void DlistPrint(pNode _head)
{
assert(_head);
if (_head->_next == _head)
{
printf("Head\n");
}
else
{
pNode pCur = _head->_next;
while (pCur != _head)
{
printf("%d--->", pCur->data);
pCur = pCur->_next;
}
if (pCur == _head)
{
printf("Head\n");
}
}
}
void TestDlist()
{
DlistInit(&_head); //初始化函数要对指针进行修改影响到外部的实参,必须传二级指针
//尾插测试
DlistPushback(_head, 1);
DlistPushback(_head, 2);
DlistPushback(_head, 3);
DlistPrint(_head);
//头插测试
DlistPushfront(_head, 0);
DlistPushfront(_head, 7);
DlistPushfront(_head, 9);
DlistPrint(_head);
//头删测试
DlistPopfront(_head);
DlistPopfront(_head);
DlistPopfront(_head);
DlistPrint(_head);
//尾删测试
DlistPopback(_head);
DlistPopback(_head);
DlistPopback(_head);
DlistPrint(_head);
//在第二个节点插入27
pNode pos = DlistFindpos(_head, 2);
printf("%p\n", pos);
DListInsert(pos, 27);
DlistPrint(_head);
pNode pos2 = DlistFindpos(_head, 2);
DListInsert(pos2, 58);
DlistPrint(_head);
//清除第二个节点
pNode pos3 = DlistFindpos(_head, 2);
DListErase(pos3);
DlistPrint(_head);
//清空链表
// DListClear(_head);
// DlistPrint(_head);
//销毁链表
// DListDestroy(&_head);
// DlistPrint(_head);
}
int main()
{
TestDlist();
system("pause");
return 0;
}