双向带头循环链表的优势,时间复杂度为O(1).方便
这里实现常见的几个接口函数
头删,尾删,头插,尾插,pos节点前插入,删除pos节点
首先定义一个双向链表,用结构体定义
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
LTDataType data;
}ListNode;
初始化函数
//初始化
ListNode* ListInit()
{
ListNode* phead = BuyList(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
扩容函数
//扩容节点
ListNode* BuyList(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
输出函数
//打印
void ListPrint(ListNode* phead)
{
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
先写查找函数(后面的pos节点前插入和删除pos节点会运用 )
//查找
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);//断言
ListNode* find = phead->next;
while (find != phead)
{
if (find->data == x)
{
return find;
}
find = find->next;
}
return 0;
}
pos节点前插入函数
//在pos前插入
void ListInsert(ListNode* pos, LTDataType x)
{
ListNode* cur = pos->prev;
ListNode* newnode = BuyList(x);
cur->next = newnode;
newnode->prev = cur;
newnode->next = pos;
pos->prev = newnode;
}
删除pos节点函数
//删除pos节点
void ListErase(ListNode* pos)
{
assert(pos);
//用来存放pos前一个节点
ListNode* first = pos->prev;
//用来存放pos后一个节点
ListNode* second = pos->next;
first->next = second;
second->prev = first;
free(pos);
pos = NULL;
}
这里将头删、尾删、头插、尾插函数放在后面是因为可以复用前面的插入和删除函数
头删函数
void ListPopFront(ListNode* phead)
{
//
//assert(phead);//断言
//assert(phead->next != phead);
定义一个指针存放第一个节点
//ListNode* first = phead->next;
定义一个指针存放第二个节点
//ListNode* second = first->next;
//phead->next = second;
//second->prev = phead;
//free(first);
//first = NULL;
//复用
ListErase(phead->next);
}
尾删函数
//尾删
void ListPopBack(ListNode* phead)
{
//assert(phead);//断言
//assert(phead->next != phead);
定义一个指针存放最后一个节点
//ListNode* first = phead->prev;
定义一个指针存放倒数第二个节点
//ListNode* second = first->prev;
//second->next = phead;
//phead->prev = second;
//free(first);
//first = NULL;
//复用
ListErase(phead->prev);
}
头插函数
//头插
void ListPushFront(ListNode* phead, LTDataType x)
{
//assert(phead);//断言
指向头节点
//ListNode* cur = phead->next;
//ListNode* newnode = BuyList(x);
//cur->prev = newnode;
//newnode->next = cur;
//newnode->prev = phead;
//phead->next = newnode;
//复用
ListInsert(phead->next, x);
}
尾插函数
//尾插
void ListPushBack(ListNode* phead, LTDataType x)
{
//assert(phead);//断言
指向尾节点
//ListNode* cur = phead->prev;
//ListNode* newnode = BuyList(x);
//cur->next = newnode;
//newnode->prev = cur;
//newnode->next = phead;
//phead->prev = newnode;
//复用
ListInsert(phead, x);
}
最后释放空间
//销毁
void ListDestory(ListNode* phead)
{
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
程序实现
void Test()
{
ListNode* plist = ListInit();
ListPushBack(plist, 1);
ListPushBack(plist, 2);
ListPushBack(plist, 3);
ListPushBack(plist, 4);
ListPrint(plist);
ListPushFront(plist, 5);
ListPrint(plist);
ListPopFront(plist);
ListPrint(plist);
ListPopBack(plist);
ListPrint(plist);
ListNode* pos = ListFind(plist, 3);
if (pos)
{
printf("找到了,并修改为40\n");
pos->data = 40;
}
else
{
printf("NO\n");
}
ListPrint(plist);
pos = ListFind(plist, 2);
if (pos)
{
ListInsert(pos, 8);
}
ListPrint(plist);
pos = ListFind(plist, 2);
if (pos)
{
ListErase(pos);
}
ListPrint(plist);
ListDestory(plist);
}
int main()
{
Test();
return 0;
}
运行结果