分为三个文件实现,头文件,函数实现文件,测试文件
头文件
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
//带头双向循环链表结构体声明
typedef struct DList
{
int data;
struct DList* prev;
struct DList* next;
}DList;
//创建返回链表的头节点
DList* ListCreate();
//创建新节点
DList* Newnode(int x);
//打印
void DLPrint(DList* head);
//尾插
void DListPushback(DList* head, int x);
//尾删
void DListPopback(DList* head);
//头插
void DListPushfront(DList* head, int x);
//头删
void DListPopfront(DList* head);
//查找
DList* DListFind(DList* head, int x);
//在pos的前面插入
void DListInsert(DList* pos, int x);
//删除pos位置的节点
void DListErase(DList* pos);
//销毁
void DListDestory(DList* head);
函数实现文件
#include"DList.h"
//创建返回链表的头节点
DList* ListCreate()
{
DList* head = (DList*)malloc(sizeof(DList));
if (head == NULL)
{
perror("create malloc fail");
return NULL;
}
head->next = head;
head->prev = head;
return head;
}
//创建新节点
DList* Newnode(int x)
{
DList* newnode = (DList*)malloc(sizeof(DList));
if (newnode == NULL)
{
perror("newnode malloc fail");
return NULL;
}
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
//打印
void DLPrint(DList* head)
{
assert(head);
DList* cur = head->next;
printf("头节点<==>");
while (cur != head)
{
printf("%d<==>", cur->data);
cur = cur->next;
}
printf("\n");
}
//尾插新节点
void DListPushback(DList* head, int x)
{
assert(head);
DList* newnode = Newnode(x);
newnode->prev = head->prev;
head->prev->next = newnode;
newnode->next = head;
head->prev = newnode;
}
//尾删
void DListPopback(DList* head)
{
assert(head);
assert(head->next!=head);
DList* del = head->prev;
DList* tailprev = head->prev->prev;
tailprev->next = head;
head->prev = tailprev;
free(del);
}
//头插
void DListPushfront(DList* head,int x)
{
assert(head);
DList* newnode = Newnode(x);
newnode->next = head->next;
head->next->prev = newnode;
newnode->prev = head;
head->next = newnode;
}
//头删
void DListPopfront(DList* head)
{
assert(head);
assert(head->next!=head);
DList* del = head->next;
DList* newfirst = head->next->next;
newfirst->prev = head;
head->next = newfirst;
free(del);
}
//查找
DList* DListFind(DList* head, int x)
{
assert(head);
assert(head->next != head);
DList* cur = head->next;
while (cur != head)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos的前面插入
void DListInsert(DList* pos, int x)
{
assert(pos);
DList* newnode = Newnode(x);
DList* pospre = pos->prev;
newnode->next = pos;
pospre->next = newnode;
newnode->prev = pospre;
pos->prev = newnode;
}
//删除pos位置的节点
void DListErase(DList* pos)
{
assert(pos);
DList* del = pos;
DList* posprev = pos->prev;
DList* posnext = pos->next;
posprev->next = posnext;
posnext->prev = posprev;
free(del);
}
//销毁
void DListDestory(DList* head)
{
DList* cur = head->prev;
DList* prev = cur->prev;
while (cur != head)
{
free(cur);
cur = prev;
prev = prev->prev;
}
free(head);
printf("销毁成功\n");
}
测试文件
#include"DList.h"
void testDLpushback()
{
printf("测试尾插:\n");
DList* head = ListCreate();
DListPushback(head, 1);
DListPushback(head, 2);
DListPushback(head, 3);
DListPushback(head, 4);
DLPrint(head);
DListDestory(head);
}
void testDLPopback()
{
printf("测试尾删:\n");
DList* head = ListCreate();
DListPushback(head, 1);
DListPushback(head, 2);
DListPushback(head, 3);
DListPushback(head, 4);
DLPrint(head);
DListPopback(head);
DListPopback(head);
DLPrint(head);
DListDestory(head);
}
void testDLPushfront()
{
printf("测试头插:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DLPrint(head);
}
void testDLPopfront()
{
printf("测试头删:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DLPrint(head);
DListPopfront(head);
DListPopfront(head);
DLPrint(head);
}
void testDListFind()
{
printf("测试查找:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DList* pos = DListFind(head, 4);
if (pos == NULL)
{
printf("在链表中未找到该节点\n");
}
else
{
printf("找到了\n");
}
DListDestory(head);
}
void testInsert()
{
printf("测试在pos位置插入:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DLPrint(head);
DList* pos = DListFind(head, 4);
DListInsert(pos, 100);
DLPrint(head);
DListDestory(head);
}
void testPosErase()
{
printf("测试删除pos节点:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DLPrint(head);
DList* pos = DListFind(head, 2);
DListErase(pos);
DLPrint(head);
DListDestory(head);
}
void testDListDestory()
{
printf("测试销毁:\n");
DList* head = ListCreate();
DListPushfront(head, 1);
DListPushfront(head, 2);
DListPushfront(head, 3);
DListPushfront(head, 4);
DLPrint(head);
DListDestory(head);
}
int main()
{
testDLpushback();
testDLPopback();
testDLPushfront();
testDLPopfront();
testDListFind();
testInsert();
testPosErase();
testDListDestory();
return 0;
}