目录
引入:带头双向循环链表:结构比较复杂,一般用在单链表存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然复杂,但是使用代码实现后就会发现结构会带来很多优势,实现起来反而简单了。 那让我们开始吧~
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDataType;//对双向循环链表创建的声明
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}ListNode;
//创建头结点
ListNode* ListInit();
//打印输出
void ListPrint(ListNode* phead);
//摧毁双向链表
void ListDestory(ListNode* phead);
//头插尾插
void ListPushFront(ListNode* phead, LTDataType x);
void ListPushBack(ListNode* phead, LTDataType x);
//头删尾删
void ListPopFront(ListNode* phead);
void ListPopBack(ListNode* phead);
//查找
ListNode* ListFind(ListNode* phead, LTDataType x);
//插入与删除节点
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);
#include"List.h"
//创建节点
ListNode* ListBuyNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->data = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}
//创建头结点
ListNode* ListInit()
{
ListNode* phead = ListBuyNode(0);
phead->next = phead;//使节点指向本身,创建双向循环链表的头节点
phead->prev = phead;
return phead;
}
//打印输出
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)//注意双向循环链表的结束标志不是NULL
{
pirntf("%d", cur->data);
cur = cur->next;
}
printf("\n");
}
尾插
头插
//尾插
void ListPushBack(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = ListBuyNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
//头插
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* first = phead->next;
ListNode* newnode = ListBuyNode(x);
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
头删
?尾删
//头删
void ListPopFront(ListNode* phead)
{
?? ?assert(phead);
?? ?ListNode* first = phead->next;
?? ?ListNode* second = first->next;
?? ?phead->next = second;
?? ?second->prev = phead;
?? ?free(first);//防止空指针的出现
?? ?first->next = NULL;
}
//尾删
void ListpopBack(ListNode* phead)
{
?? ?assert(phead);
?? ?assert(phead->next != phead);//防止头结点被删除
?? ?ListNode* tail = phead->prev;
?? ?ListNode* prev = tail->prev;
?? ?prev->next = phead;
?? ?phead->prev = prev;
?? ?free(tail);//防止空指针的出现
?? ?tail->next = NULL;
}
//查找节点
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data = x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
?插入
删除
//插入节点
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = ListBuyNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//删除节点
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
pos->next = NULL;
}
//删除链表
void LsitDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
phead = NULL;
}
博客到这里也是结束了,喜欢的小伙伴可以点赞加关注支持下博主,这对我真的很重要~~?