双向循环链表的接口
目录
头文件
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
创建链表、节点
// 指的是双向循环链表中的结点有效数据类型,用户可以根据需要进行修改hj
typedef int DataType_t;
// 构造双向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleCircLinkedList
{
DataType_t data; // 结点的数据域
struct DoubleCircLinkedList *prev; // 直接前驱的指针域
struct DoubleCircLinkedList *next; // 直接后继的指针域
} DoubleCircLList_t;
/********************************************************************
*
* name : DoubleCircLList_HeadInsert
* function : 创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
* argument :
* @data:节点需存储的数据
* retval : 返回新创建链表头节点的地址
* author : 17647576169@163.com
* date : 2024-4-24
* note : None
*
* *****************************************************************/
DoubleCircLList_t *DoubleCircCirLList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
DoubleCircLList_t *Head = (DoubleCircLList_t *)calloc(1, sizeof(DoubleCircLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
Head->prev = Head;
Head->next = Head;
// 3.把头结点的地址返回即可
return Head;
}
/********************************************************************
*
* name : DoubleCircLList_HeadInsert
* function : 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
* argument :
* @data:节点需存储的数据
* retval : 返回新创建节点的地址
* author : 17647576169@163.com
* date : 2024-4-24
* note : None
*
* *****************************************************************/
DoubleCircLList_t *DoubleCircCirLList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
DoubleCircLList_t *New = (DoubleCircLList_t *)calloc(1, sizeof(DoubleCircLList_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;
}
三种插入方式
在链表的头部插入
/********************************************************************
*
* name : DoubleLList_HeadInsert
* function : 向链表的头部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回新创建链表节点的地址
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_HeadInsert(DoubleLList_t *Head, DataType_t data)
{
// 备份头节点
DoubleLList_t *Phead = Head;
// 1.创建新的结点,并对新结点进行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的头部
New->next = Head->next;
Head->prev = New;
Head->next = New;
return true;
}
在链表的尾部插入
/********************************************************************
*
* name : LList_TailInsert
* function : 向链表的尾部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_TailInsert(DoubleLList_t *Head, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3对链表的头文件的地址进行备份
DoubleLList_t *Phead = Head;
// 4遍历链表找到尾部
while (Phead->next)
{
// 把头的直接后继作为新的头结点
Phead = Phead->next;
}
// 5.把新结点插入到链表的尾部
Phead->next = New;
New->prev = Phead;
return true;
}
在链表指定数据节点后插入
/********************************************************************
*
* name : DoubleLList_DestInsert
* function : 向链表的指定位置后进行插入
* argument : @head:目标链表
* @data:需插入的数据
* @dest:插入位置
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_DestInsert(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
DoubleLList_t *New = DoubleLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空
if (NULL == Head->next)
{
return false;
}
// 备份头节点地址
DoubleLList_t *Phead = Head;
DoubleLList_t *P = NULL;
// 找目标数据
while (NULL != Phead->next)
{
Phead = Phead->next;
if (destval == Phead->data)
{
P = Phead;
break;
}
}
// 找不到目标数据
if (NULL == P)
{
perror("The target data cannot be found\n");
return false;
}
// 目标数据在尾部
if (NULL == Phead->next)
{
DoubleLList_TailInsert(Head, data);
return true;
}
// 目标数据在头部
if (Head->next == Phead)
{
DoubleLList_HeadInsert(Head, data);
return true;
}
// 开始插入
New->next = Phead->next;
Phead->next->prev = New;
New->prev = Phead;
Phead->next = New;
return true;
}
三种删除方式
删除链表头部的节点
/********************************************************************
*
* name : LList_DestInsert
* function : 头删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
DoubleLList_t *Phead = Head->next;
// 1.判断链表是否为空,如果为空
if (NULL == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 备份首节点地址
// 链表只有一个节点
if (NULL == Phead->next)
{
free(Phead);
Head->next = NULL;
return true;
}
// 删除
Head->next = Phead->next;
Phead->next->prev = NULL;
Phead->next = NULL;
free(Phead);
return true;
}
删除链表尾部的节点
/********************************************************************
*
* name : LList_DestInsert
* function : 尾删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
// 1.判断链表是否为空,如果为空
if (NULL == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 备份首节点地址
DoubleLList_t *Phead = Head->next;
// 链表只有一个节点
if (NULL == Phead->next)
{
free(Phead);
Head->next = NULL;
return true;
}
// 遍历找到尾结点
while (NULL != Phead->next)
{
Phead = Phead->next;
}
Phead->prev->next = NULL;
Phead->prev = NULL;
free(Phead);
return true;
}
删除链表指定数据节点后的节点
/********************************************************************
*
* name : DoubleCircLList_Del
* function : 中删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失/********************************************************************
*
* name : LList_DestInsert
* function : 中删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool DoubleLList_Del(DoubleLList_t *Head, DataType_t data)
{
// 1.判断链表是否为空,如果为空
if (NULL == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 删除的节点在头
if (Head->next->data == data)
{
DoubleLList_HeadDel(Head);
return true;
}
// 遍历到尾结点
DoubleLList_t *P = NULL;
// 备份头节点地址
DoubleLList_t *Phead = Head;
while (NULL != Phead->next)
{
Phead = Phead->next;
if (data == Phead->data)
{
P = Phead;
break;
}
}
// 找不到目标数据
if (NULL == P)
{
perror("The target data cannot be found\n");
return false;
}
// 目标数据在尾部
if (NULL == Phead->next)
{
DoubleLList_TailDel(Head);
return true;
}
Phead->prev->next = Phead->next;
Phead->next->prev = Phead->prev;
Phead->prev = NULL;
Phead->next = NULL;
free(Phead);
return true;
}败
* author : 17647576169@163.com
* date : 2024-4-24
* note :
*
* *****************************************************************/
bool DoubleCircLList_Del(DoubleCircLList_t *Head, DataType_t data)
{
// 备份首节点地址
DoubleCircLList_t *Phead = Head;
// 备份头节点地址
DoubleCircLList_t *Temp = Head->next;
// 1.判断链表是否为空,如果为空
if (Head == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 删除的节点在头
if (data == Head->next->data)
{
DoubleCircLList_HeadDel(Head);
return true;
}
// 遍历找的目标数据节点
DoubleCircLList_t *P = NULL; // 定义变量保存找的目标数据的节点地址
while (Head->next != Temp->next)
{
Temp = Temp->next;
if (data == Temp->data)
{
P = Temp;
break;
}
}
// 找不到目标数据
if (NULL == P)
{
perror("The target data cannot be found\n");
return false;
}
// 目标数据在尾部
if (Head->next == P->next)
{
DoubleCircLList_TailDel(Head);
}
else
{
P->next->prev = P->prev; // 删除节点的后继指向删除节点的前驱
P->prev->next = P->next; // 删除节点的前驱指向删除节点的后继
P->next = NULL; // 删除节点的首指针释放
P->prev = NULL; // 删除节点的尾指针释放
free(P); // 释放删除节点的堆内存
}
return true;
}
遍历
/********************************************************************
*
* name : LList_Print
* function : 遍历链表
* argument : @head:目标链表
*
*
* retval : none
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
void DoubleLList_Print(DoubleLList_t *Head)
{
// 对链表的头文件的地址进行备份
DoubleLList_t *Phead = Head;
// 首结点
while (Phead->next)
{
// 把头的直接后继作为新的头结点
Phead = Phead->next;
// 输出头结点的直接后继的数据域
printf("data = %d\n", Phead->data);
}
}
验证
int main(int argc, char const *argv[])
{
// 1.创建顺序表
DoubleLList_t *Manager = DoubleLList_Create();
// 2.向顺序表中的尾部插入新元素
DoubleLList_TailInsert(Manager, 5);
DoubleLList_TailInsert(Manager, 2);
DoubleLList_TailInsert(Manager, 1);
DoubleLList_TailInsert(Manager, 4);
DoubleLList_TailInsert(Manager, 6);
// 3.遍历顺序表
DoubleLList_Print(Manager); // -- 5 2 1 4 6
printf("\n");
// 4.向顺序表中的头部插入新元素
DoubleLList_HeadInsert(Manager, 9);
DoubleLList_HeadInsert(Manager, 7);
DoubleLList_HeadInsert(Manager, 8);
DoubleLList_HeadInsert(Manager, 0);
DoubleLList_HeadInsert(Manager, 10);
// cha
DoubleLList_DestInsert(Manager, 8, 0);
// 5.遍历顺序表
DoubleLList_Print(Manager); // --10 0 8 0 7 9 5 2 1 4 6
printf("\n");
// 头删除
DoubleLList_HeadDel(Manager);
// 遍历顺序表
DoubleLList_Print(Manager); // --0 8 0 7 9 5 2 1 4 6
printf("\n");
// 尾删除
DoubleLList_TailDel(Manager);
// 遍历顺序表
DoubleLList_Print(Manager); // --0 8 0 7 9 5 2 1 4
printf("\n");
// 删除顺序表的元素
DoubleLList_Del(Manager, 2);
// 遍历顺序表
DoubleLList_Print(Manager); // --0 8 7 9 5 2 1 4
printf("\n");
return 0;
}
输出结果
data = 5
data = 2
data = 1
data = 4
data = 6
data = 10
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 1
data = 4
玄机博客
© 版权声明
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
THE END
暂无评论内容