双向循环链表接口

双向循环链表接口

/***********************************************************
 *
 *    file name:     DoubleCircularLinkedList  interface
 *    author   :     19870326073@163.com
 *    date     :     2024/04/23
 *    function :     Make great CV engineer
 *    note     :     
 *
 *    CopyRight (c)  2023-2024  19870326073@163.com  All Right Resever
 *
 * ***********************************************************/

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

/***********************************************************
 *
 *    函数名称:     DoubleCircularLinkedList  interface
 *    函数功能:
 *    函数参数:
 *
 *
 *    返回结果:
 *    注意事项:     None
 *    函数作者:     19870326073@163.com
 *    创建日期:     2024/04/25
 *    修改历史:
 *    函数版本:     V1.0
 * ***********************************************************/

// 指的是双向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;

// 构造双向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleCircularLinkedList
{
    DataType_t data;                       // 结点的数据域
    struct DoubleCircularLinkedList *prev; // 直接前驱的指针域
    struct DoubleCircularLinkedList *next; // 直接后继的指针域

} DoubCircLList_t;

// 创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
DoubCircLList_t *DoubleCircularLinkedList_Create(void)
{
    // 1.创建一个头结点并对头结点申请内存
    DoubCircLList_t *Head = (DoubCircLList_t *)calloc(1, sizeof(DoubCircLList_t));
    if (NULL == Head)
    {
        perror("Calloc memory for Head is Failed");
        exit(-1);
    }
    // 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
    Head->prev = Head;
    Head->next = Head;
    // 3.把头结点的地址返回即可
    return Head;
}
// 创建新的结点,并初始化新的结点
DoubCircLList_t *DoubleCircularLinkedList_NewNode(DataType_t data)
{
    // 1.创建一个新的结点并给新结点申请内存
    DoubCircLList_t *New = (DoubCircLList_t *)calloc(1, sizeof(DoubCircLList_t));
    if (NULL == New)
    {
        perror("Calloc memory for NewNode is Failed");
        return NULL;
    }

    // 对新结点的数据域和指针域(两个指针域)进行初始化,指针域指向结点自身,体现“循环”
    New->data = data;
    New->prev = New;
    New->next = New;
    return New;
}
/********************************************************************************
 *
 *Function name:DoubCircLList_t_HeadInsert
 *Function function:DoubleCircularLinkedList HeadInsert
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_HeadInsert(DoubCircLList_t *Head, DataType_t data)
{
    DoubCircLList_t *Phead = Head->next;
    DoubCircLList_t *new = DoubleCircularLinkedList_NewNode(data);
    if (Head->next == Head) // 如果链表为空链表
    {
        Head->next = new;
        return true;
    }
    Phead->prev->next = new;
    new->prev = Phead->prev;
    new->next = Phead;
    Phead->prev = new;
    Head->next = new;
    return true;
}

/********************************************************************************
 *
 *Function name:DoubCircLList_t_TailInsert
 *Function function:DoubleCircularLinkedList TailInsert
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_TailInsert(DoubCircLList_t *Head, DataType_t data)
{
    DoubCircLList_t *Phead = Head->next->prev; // 临时记录尾结点的地址
    DoubCircLList_t *new = DoubleCircularLinkedList_NewNode(data);
    if (Head->next == Head) // 如果链表为空链表
    {
        Head->next = new;
        return true;
    }
    new->prev = Phead; // Phead;原来的尾结点
    Phead->next = new;
    new->next = Head->next;
    Head->next->prev = new;
    return true;
}

/********************************************************************************
 *
 *Function name:DoubCircLList_t_DestInsert
 *Function function:DoubleCircularLinkedList DestInsert
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_DestInsert(DoubCircLList_t *Head, DataType_t data, DataType_t destval)
{
    DoubCircLList_t *Phead = Head->next;
    DoubCircLList_t *new = DoubleCircularLinkedList_NewNode(data);
    if (Head->next = Head) // 如果链表为空链表
    {
        printf("LinkList is empty");
        return false;
    }
    while (destval != Phead->data) // 判断是否有目标值,没有的话遍历寻找
    {
        Phead = Phead->next;     // 遍历
        if (Phead == Head->next) // 到尾结点了还没有找到则printf
        {
            printf("There is no destination value"); // 找不到目标值,退出return false
            return false;
        }
    }

    // 有目标值的情况
    new->next = Phead->next; // 进行插入操作,因为是循环,因此头插,尾插都涵盖
    Phead->next->prev = new;
    new->prev = Phead;
    Phead->next = new;
    return true;
}

/********************************************************************************
 *
 *Function name:DoubCircLList_t_HeadDelet
 *Function function:DoubleCircularLinkedList HeadDelet
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_HeadDelet(DoubCircLList_t *Head)
{
    // 对首结点的地址进行备份
    DoubCircLList_t *Phead = Head->next;
    if (Head->next == Head) // 如果链表为空链表则直接退出判断
    {
        printf("LinkList is empty");
        return false;
    }
    if (Head->next->next == Head->next) // 判断当前链表是否只有一个元素
    {
        Phead->next = NULL;
        Phead->prev = NULL;
        Head->next = Head;

        free(Phead);
    }
    else // delete the head 普通情况,则进行删除首节点操作
    {
        Phead->prev->next = Phead->next;
        Phead->next->prev = Phead->prev;
        Phead->next = NULL;
        Phead->prev = NULL;
        Head->next = Phead->next;

        free(Phead);
    }
    return true;
}

/********************************************************************************
 *
 *Function name:DoubCircLList_t_TailDelet
 *Function function:DoubleCircularLinkedList TailDelet
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_TailDelet(DoubCircLList_t *Head)
{
    // 对尾结点的地址进行备份
    DoubCircLList_t *Phead = Head->next;
    if (Head->next == Head) // 如果链表为空链表则直接退出判断
    {
        printf("LinkList is empty");
        return false;
    }
    if (Head->next->next == Head->next) // 判断当前链表是否只有一个元素
    {
        Phead->next = NULL;
        Phead->prev = NULL;
        Head->next = Head;
        free(Phead);
    }
    else // delete the head 普通情况,则进行删除尾节点
    {
        Phead->prev->next = Head->next;
        Phead->next->prev = Phead->prev;
        Phead->next = NULL;
        Phead->prev = NULL;

        free(Phead);
    }
    return true;
}

/********************************************************************************
 *
 *Function name:DoubCircLList_t_DestDelet
 *Function function:DoubleCircularLinkedList DestDelet
 *Function parameter:
 *Return the result:bool:   ture    or    false
 **atters needing attention:19870326073@163.com
 *Function author:
 *Creation date:2024/04/25
 *Modify history:
 *Function version:v1.0
 *
 *******************************************************************************/

bool DoubCircLList_t_DestDelet(DoubCircLList_t *Head, DataType_t destval)
{
    DoubCircLList_t *Phead = Head->next; // 对链表的当前结点的地址进行备份
    if (Head->next == Head)              // 判断当前链表是否为空,为空则直接退出
    {
        printf("LinkList is empty");
        return false;
    }
    while (Phead->data != destval) //找到指定值的节点
    {
        Phead = Phead->next;
        if (Phead == Head->next)
        {
            printf("There is no destination value.\n"); // 遍历完成以后仍找不到目标值,就退出
            return false;
        }
    }
    if (Phead == Head->next) // 删除目标值位于首节点时
    {
        if (Phead->next == Head->next) // 链表仅单个元素时
        {
            Head->next = Phead;
            Phead->prev = NULL;
            Phead->next = NULL;
        }
        else // 链表不止单个元素时
        {
            Phead->prev->next = Phead->next;
            Phead->next->prev = Phead->next;
            Head->next = Phead->next;
            Phead->prev = NULL;
            Phead->next = NULL;
        }
        free(Phead);
    }
    else // 如果存在目标值,且目标值不位于首节点时,进行删除操作
    {
        Phead->prev->next = Phead->next;
        Phead->next->prev = Phead->prev;
        Phead->prev = NULL;
        Phead->next = NULL;
        free(Phead);
    }
    return true;
}

posted @   头像被封了  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示