DList.h的内容:
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int DLTDataType ;
typedef struct DListNode
{
struct DListNode* next;
struct DListNode* prev;
DLTDataType data;
}DLTNode;
DLTNode* DListInit();
void DListPrint(DLTNode* phead);
void DListPushFront(DLTNode* phead, DLTDataType x);//头插
void DListPushBack(DLTNode* phead, DLTDataType x);//尾插
void DListPopFront(DLTNode* phead);//头删
void DListPopBack(DLTNode* phead);//尾删
bool DListEmpty(DLTNode* phead);
size_t DListSize(DLTNode*phead);
DLTNode* DListFind(DLTNode* phead, DLTDataType x);
//在pos之前
void DListInsert(DLTNode* pos, DLTDataType x);
//删除pos位置
void DListEra(DLTNode* pos);
Dlist.c的内容
#include"DList.h"
DLTNode* DListInit()
{
DLTNode* guard = (DLTNode*)malloc(sizeof(DLTNode));
if (guard == NULL)
{
perror("malloc fail");
exit(-1);
}
guard->next = guard;
guard->prev = guard;
return guard;
}
DLTNode* BuyListNode(DLTDataType x)
{
DLTNode* node = (DLTNode*)malloc(sizeof(DLTNode));
if (node == NULL)
{
perror("malloc fail");
exit(-1);
}
node->next = NULL;
node->prev = NULL;
node->data = x;
return node;
}
void DListPrint(DLTNode* phead)
{
assert(phead);
printf("phead<=>");
DLTNode* cur = phead->next;
while (cur != phead)
{
printf("%d<=>", cur->data);
cur = cur->next;
}
printf("\n");
}
void DListPushBack(DLTNode* phead, DLTDataType x)//尾插
{
assert(phead);
DLTNode* newnode = BuyListNode(x);
DLTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
//DListInsert(phead->next, x);
}
void DListPushFront(DLTNode* phead, DLTDataType x)//头插
{
assert(phead);
//先链接newnode和phead->next节点的关系
/*DLTNode* newnode = BuyListNode(x);
DLTNode* first = phead->next;
newnode->next = phead->next;
phead->next->prev = newnode;
newnode->prev = phead;*/
//不关心顺序
DLTNode* newnode = BuyListNode(x);
DLTNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
void DListPopFront(DLTNode* phead)//头删
{
assert(phead);
assert(!DListEmpty(phead));
DLTNode* first = phead->next;
DLTNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);
first = NULL;
//DListEra(phead->next);
}
void DListPopBack(DLTNode* phead)//尾删
{
assert(phead);
assert(!DListEmpty(phead));
DLTNode* tail = phead->next;
DLTNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
//DListEra(phead->prev);
}
bool DListEmpty(DLTNode* phead)
{
assert(phead);
return phead->next == phead;
}
size_t DListSize(DLTNode* phead)
{
assert(phead);
size_t n = 0;
DLTNode* cur = phead->next;
while (cur != phead)
{
++n;
cur = cur->next;
}
return n;
}
DLTNode* DListFind(DLTNode* phead, DLTDataType x)
{
assert(phead);
size_t n = 0;
DLTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return n;
}
//在pos之前
void DListInsert(DLTNode* pos, DLTDataType x)
{
assert(pos);
DLTNode* prev = pos->prev;
DLTNode* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//删除pos位置
void DListEra(DLTNode* pos)
{
assert(pos);
DLTNode* prev = pos->next;
DLTNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
//可以传二级指针,内部置空头结点
//建议:也可以考虑一级指针,让调用的DListDestory人置空(保持接口的一致性)
void DListDestory(DLTNode* phead)//释放链表
{
assert(phead);
DLTNode* cur = phead->next;
while (cur!=phead)
{
DLTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
test.c的内容:
#include"DList.h"
void TestList1()
{
//DLTNode* plist = NULL;
DLTNode* plist = DListInit();
DListPushBack(plist, 1);
DListPushBack(plist, 2);
DListPushBack(plist, 3);
DListPushBack(plist, 4);
DListPrint(plist);
DListPushFront(plist, 10);
DListPushFront(plist, 20);
DListPushFront(plist, 30);
DListPushFront(plist, 40);
DListPrint(plist);
DListPopBack(plist);
DListPopBack(plist);
DListPopBack(plist);
DListPopBack(plist);
DListPrint(plist);
DListDestory(plist);
}
void TestList2()
{
DLTNode* plist = DListInit();
DListPushBack(plist, 1);
DListPushBack(plist, 2);
DListPushBack(plist, 3);
DListPushBack(plist, 4);
DListPrint(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPrint(plist);
DListPopFront(plist);
DListPopFront(plist);
DListPrint(plist);
DListDestory(plist);
plist = NULL;
}
int main()
{
//TestList1();
TestList2();
return 0;
}
运行一下TestList2():