双向循环链表的插入和删除
数据结构
线性表--双向循环链表操作
**注意!!!**怎么说,今天(2024.4.24)找一个小小的运行bug(没有报错)找了非常之久,明天继续把这些代码补齐,啊啊啊,但是感谢还是把这个bug找出来(这段话我不会删的)~~
测试该程序的相关头文件和定义以及相关函数 |
---|
最后一个代码块。 |
/****************************************************************************
*
* file name: doublecirlinkedlist.c
* author : tongyaqi1110@163.com
* date : 2024-04-24
* function : 实现双向循环表的插入和删除
* note : None
* CopyRight (c) 2024 tongyaqi1110@163.com Right Reseverd
*
****************************************************************************/
·插入
1)头插
/****************************************************************************
*
* function name : DoubleCirLList_HeadInsert
* function : 创建新的结点,并进行头插法
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@.com
* date : 2024-04-24
* version : V1.0
* revision history : None
* Copyright (c) 2023-2024 cececlmx@126.com All right Reserved
****************************************************************************/
bool DoubleCirLList_HeadInsert(DoubleCirLList_t *Head, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的头部
Head->next->prev->next = New;
New->prev = Head->next->prev;
New->next = Head->next;
Head->next->prev = New;
Head->next = New;
return true;
}
2)尾插
/****************************************************************************
*
* function name : DoubleCirLList_TailInsert
* function : 创建新的结点,并进行尾插法
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
bool DoubleCirLList_TailInsert(DoubleCirLList_t *Head, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的尾部
New->prev = Head->next->prev; // 新结点的前驱连接上原来的尾结点
New->prev->next = New; // 原来的尾结点连上新结点
Head->next->prev = New; // 首节点连接上新的尾结点
New->next = Head->next; // 新的尾结点连接上首节点
return true;
}
3)中间插(指定插)
/****************************************************************************
*
* function name : DoubleCirLList_DestInsert
* function : 创建新的结点,并插入双向循环链表指定节点后面(中间插)
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
bool DoubleCirLList_DestInsert(DoubleCirLList_t *Head, DataType_t dest, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
return true;
}
// printf("Please input your point dest_data: ");
// scanf("%d", &data);
// 3.如果链表为非空,开始寻找目标结点
// 1)我们开始进行遍历
DoubleCirLList_t *Phead = Head; // 定义变量备份头结点地址
while (Head->next)
{
Phead = Phead->next; // 遍历,移动结点
// 如果目标结点等于带遍历寻找结点或者要寻找的结点就是首结点,就退出
if (dest == Phead->data || Phead->next == Head->next)
{
break;
}
}
// 如果找不到目标结点,会有提示找不到
if (dest != Phead->data)
{
printf("not find DoubleLList_DestInsert value[%d]\n", dest);
return false;
}
// 如果找到了目标结点,按照以下步骤进行连接
New->next = Phead->next; // 新结点连接上要插入结点原来后面的结点
Phead->next->prev = New; // 要插入结点原来后面的结点连接上新结点
New->prev = Phead; // 指定结点指向新结点
Phead->next = New; // 新结点指向指定结点
return true;
}
·删除
1)头删
"只含有头结点的双向循环链表"
/****************************************************************************
*
* function name : DoubleCirLList_HeadDel
* function : 删除双向循环链表的首节点(头删)
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
bool DoubleCirLList_HeadDel(DoubleCirLList_t *Head)
{
// 1.判断链表是否为空,如果为空,退出
if (Head == Head->next)
{
printf("DoubleCirLList is empty\n");
return false;
}
// 2.如果链表为非空,则删除链表的首结点
DoubleCirLList_t *Phead = Head->next; // 备份首结点
// 1)如果首结点为唯一的含数据的结点
if (Head->next == Phead->next)
{
Phead->next = NULL;
Phead->prev = NULL;
Head->next = Head; // 见只含有头结点的双向循环链表图
free(Phead);
}
// 2)如果首结点为不是唯一的含数据的结点
else
{
Phead->prev->next = Phead->next; // 尾节点的next指向首结点的直接后继结点
Phead->next->prev = Phead->prev; // 首结点的直接后继指向尾结点
Head->next = Phead->next; // 让头结点指向首结点的直接后继,这个顺序不能变
Phead->next = NULL; // 断开首结点与后继结点的联系
Phead->prev = NULL; // 断开首结点与前驱结点的联系
// Head->next = Phead->next; 如果在phead的左右都断了,那么head也找不首结点的后继在哪里了
free(Phead); // 此时首结点已经不在与任何结点有联系,便释放
}
return true;
}
2)尾删
/****************************************************************************
*
* function name : DoubleCirLList_TailDel
* function : 删除双向循环链表的尾节点(尾删)
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
bool DoubleCirLList_TailDel(DoubleCirLList_t *Head)
{
// 1.判断链表是否为空,如果为空,退出
if (Head == Head->next)
{
printf("DoubleCirLList is empty\n");
return false;
}
// 2.如果链表为非空,则删除链表的首结点
DoubleCirLList_t *Thead = Head->next->prev; // 备份首结点
// 1)如果首结点为唯一的含数据的结点
if (Head->next == Head->next->next)
{
Thead->next = NULL;
Thead->prev = NULL;
Head->next = Head; // 见只含有头结点的双向循环链表图
free(Thead);
}
// 2)如果首结点为不是唯一的含数据的结点
else
{
Thead->prev->next = Head->next; // 要删结点的前面一个结点连接上首结点
Head->next->prev = Thead->prev; // 首结点连接上要删结点的前面一个结点
Thead->next = NULL; // 断开要删节点的直接后继
Thead->prev = NULL; // 断开要删结点的直接前驱
free(Thead);
}
return true;
}
3)中间删
/****************************************************************************
*
* function name : DoubleCirLList_DestDel
* function : 删除双向循环链表的中间指定节点(指定删)
* parameter : None
*
* Return results : 返回成功或者失败。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
bool DoubleCirLList_DestDel(DoubleCirLList_t *Head, DataType_t dest)
{
// 1.判断链表是否为空,如果为空,则直接退出
if (Head == Head->next)
{
printf("DoubleCirLList is Empty\n");
return false;
}
// 2.开始寻找目标节点
DoubleCirLList_t *Phead = Head; // 定义变量备份头结点地址
while (Head->next)
{
Phead = Phead->next; // 遍历,移动结点,即届时Phead为删除的目标结点
// 如果目标结点等于带遍历寻找结点或者要寻找的结点就是首结点,就退出
if (dest == Phead->data || Phead->next == Head->next)
{
break;
}
}
// 如果找不到目标结点,会有提示找不到
if (dest != Phead->data)
{
printf("not find DoubleLList_DestInsert value[%d]\n", dest);
return false;
}
// 如果找到了目标结点,按照以下步骤进行连接,即按中间删除步骤进行
else
{
Phead->prev->next = Phead->next; // 要删节点的前驱结点指向要删结点的后继结点
Phead->next->prev = Phead->prev; // 新的后继结点指向要删结点的前驱
Phead->next = NULL; // 要删结点的后继释放
Phead->prev = NULL; // 要删结点的前驱释放
free(Phead);
}
return true;
}
最后一个代码块
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
// 指的是双向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造双向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleCirLinkedList
{
DataType_t data; // 结点的数据域
struct DoubleCirLinkedList *prev; // 直接前驱的指针域
struct DoubleCirLinkedList *next; // 直接后继的指针域
} DoubleCirLList_t;
/****************************************************************************
*
* function name : DoubleCirLList_Create
* function : 创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
* parameter : None
*
* Return results : 返回头结点地址
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
DoubleCirLList_t *DoubleCirLList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
DoubleCirLList_t *Head = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
Head->prev = Head;
Head->next = Head;
// 3.把头结点的地址返回即可
return Head;
}
/****************************************************************************
*
* function name : DoubleCirLList_NewNode
* function : 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
* parameter : None
*
* Return results : 返回新结点地址。
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
DoubleCirLList_t *DoubleCirLList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
DoubleCirLList_t *New = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
// 2.对新结点的数据域和指针域(2个)进行初始化,指针域指向结点自身,体现“循环”
New->data = data;
New->prev = New;
New->next = New;
return New;
}
/****************************************************************************
*
* function name : DoubleCirLList_Print
* function : 遍历输出链表节点的数据
* parameter : None
*
* Return results : None
* note : None
* author : tongyaqi1110@163.com
* date : 2024-04-24
* version : V1.0
* revision history : None
*
****************************************************************************/
// 遍历
void DoubleCirLList_Print(DoubleCirLList_t *Head)
{
// 对链表的头文件的地址进行备份
DoubleCirLList_t *Phead = Head;
// 判断当前链表是否为空,为空则直接退出
if (Head->next == Head)
{
printf("DoubleCircular current linkedlist is empty!\n");
return;
}
// 首结点
while (Phead->next)
{
// 把头的直接后继作为新的头结点,Phead一直往后移动成为尾结点
Phead = Phead->next;
// 输出头结点的直接后继的数据域,当数据不再插入时,Phead指针指向头节点即为终止条件
printf("data = %d\n", Phead->data);
if (Phead->next == Head->next)
{
break;
}
}
}