目录
函数头文件doublelist.h
#pragma once
#include<stdio.h>
#include<assert.h>
#define DLTDataType int
typedef struct DListNode DListNode;
struct DListNode
{
DListNode* prev;
DListNode* next;
DLTDataType date;
};
DListNode* BuyDListNode(DLTDataType x);//建立一个新的节点
void DListNodePrint(DListNode* plist);//打印链表
void DListPushFront(DListNode* plist, DLTDataType x);//链表前端插入
void DListPushBack(DListNode* plist, DLTDataType x);//链表后端插入
void DListPopFront(DListNode* plist);//链表前端删除
void DListPopBack(DListNode* plist);//链表后端删除
DListNode* DListFind(DListNode* plist, DLTDataType x);//找到想要的节点地址
void DListInsertBefore( DListNode* pos, DLTDataType x);//节点前插入
void DListInsertAfter(DListNode* pos, DLTDataType x);//节点后插入
void DListEraseAfter(DListNode* plist, DListNode* pos);//节点后删除
void DListEraseBefore(DListNode* plist, DListNode* pos);//节点前删除
void DistroyDList(DListNode* plist);
函数定义文件doublelist.c
#include"doublelist.h"
DListNode* BuyDListNode(DLTDataType x)
{
DListNode* newnode = (DListNode*)malloc(sizeof(DListNode));
newnode->date = x;
newnode->next = newnode;
newnode->prev = newnode;
return newnode;
}
void DListPushFront(DListNode* plist, DLTDataType x)
{
assert(plist);
DListNode* newnode = BuyDListNode(x);
newnode->next = plist->next;
newnode->next->prev = newnode;
plist->next = newnode;
newnode->prev = plist;
}
void DListPushBack(DListNode* plist, DLTDataType x)
{
assert(plist);
DListNode* newnode = BuyDListNode(x);
plist->prev->next = newnode;
newnode->prev = plist->prev;
newnode->next = plist;
plist->prev = newnode;
}
void DListPopFront(DListNode* plist)
{
assert(plist);
if (plist->next == plist)
{
return;
}
DListNode* cur = plist->next;
plist->next = cur->next;
cur->next->prev = plist;
free(cur);
cur = NULL;
}
void DListPopBack(DListNode* plist)
{
assert(plist);
if (plist->next == plist)
{
return;
}
DListNode* cur = plist->prev;
plist->prev = cur->prev;
cur->prev->next = plist;
free(cur);
cur = NULL;
}
void DListNodePrint(DListNode* plist)
{
assert(plist);
if (plist->next == plist)
{
return;
}
DListNode* cur = plist->next;
while (cur != plist)
{
printf("%d->", cur->date);
cur = cur->next;
}
printf("NULL\n");
}
DListNode* DListFind(DListNode* plist, DLTDataType x)
{
assert(plist);
DListNode* cur = plist->next;
while (cur != plist)
{
if (cur->date == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void DListInsertBefore( DListNode* pos, DLTDataType x)
{
DListNode* prev = pos->prev;
DListNode* newnode = BuyDListNode(x);
newnode->prev = prev;
newnode->next = pos;
prev->next = newnode;
pos->prev = newnode;
}
void DListInsertAfter(DListNode* pos, DLTDataType x)
{
DListNode* next = pos->next;
DListNode* newnode = BuyDListNode(x);
newnode->prev = pos;
newnode->next = next;
pos->next = newnode;
next->prev = newnode;
}
void DListEraseAfter(DListNode* plist, DListNode* pos)
{
if (pos->next == plist)
{
return;
}
DListNode* after = pos->next->next;
DListNode* cur = pos->next;
pos->next = after;
after->prev = pos;
free(cur);
cur = NULL;
}
void DListEraseBefore(DListNode* plist, DListNode* pos)
{
if (pos->prev == plist)
{
return;
}
DListNode* before = pos->prev->prev;
DListNode* cur = pos->prev;
before->next = pos;
pos->prev = before;
free(cur);
cur = NULL;
}
void DistroyDList(DListNode* plist)
{
assert(plist);
while (plist->next != plist)
{
DListPopBack(plist);
}
free(plist);
plist = NULL;
}
调试主函数文件main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"doublelist.h"
void test1()
{
DListNode* plist = BuyDListNode(-1);
DListPushFront(plist, 1);
DListPushFront(plist, 2);
DListPushFront(plist, 3);
DListPushFront(plist, 4);
DListNodePrint(plist);
DListPushBack(plist, 1);
DListPushBack(plist, 2);
DListPushBack(plist, 3);
DListPushBack(plist, 4);
DListNodePrint(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPopFront(plist);
DListNodePrint(plist);
DListPopBack(plist);
DListNodePrint(plist);
DListNode* pos = DListFind(plist, 3);
DListInsertBefore(pos, 6);
DListInsertBefore(pos, 7);
DListInsertBefore(pos, 8);
DListInsertBefore(pos, 9);
DListNodePrint(plist);
DListInsertAfter(pos, 5);
DListInsertAfter(pos, 1);
DListNodePrint(plist);
DListEraseAfter(plist, pos);
DListNodePrint(plist);
DListEraseBefore(plist, pos);
DListNodePrint(plist);
DistroyDList(plist);
plist = NULL;
}
int main()
{
test1();
return 0;
}