双向链表的接口设计

/**

  • @file name: main.c
  • @brief 双向链表的接口设计
  • @author 1810866453@163.com
  • @date 2024/04/23
  • @version 1.0 :版本
  • @property :属性介绍
  • @note 补充 注意 说明
  • CopyRight (c) 2023-2024 RISE_AND_GRIND@163.com All Right Reseverd
    */

include <stdio.h>

include <stdbool.h>

include <stdlib.h>

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

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

} DoubleLList_t;

/**

  • @function name: DoubleLList_Create

  • @brief 创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化

  • @param 介绍函数参数 void

  • @retval void

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    // 创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化
    DoubleLList_t *DoubleLList_Create(void)
    {
    // 1.创建一个头结点并对头结点申请内存
    DoubleLList_t *Head = (DoubleLList_t *)calloc(1, sizeof(DoubleLList_t));
    if (NULL == Head)
    {
    perror("Calloc memory for Head is Failed");
    exit(-1);
    }

    // 2.对头结点进行初始化,头结点是不存储数据域,指针域指向NULL
    Head->prev = NULL;
    Head->next = NULL;

    // 3.把头结点的地址返回即可
    return Head;
    }

// 创建新的结点,并对新结点进行初始化(数据域 + 指针域)

/**

  • @function name: DoubleLList_NewNode

  • @brief // 1.创建一个新结点并对新结点申请内存

  • @param 介绍函数参数 @data

  • @retval void

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    DoubleLList_t *DoubleLList_NewNode(DataType_t data)
    {
    // 1.创建一个新结点并对新结点申请内存
    DoubleLList_t *New = (DoubleLList_t *)calloc(1, sizeof(DoubleLList_t));
    if (NULL == New)
    {
    perror("Calloc memory for NewNode is Failed");
    return NULL;
    }

    // 2.对新结点的数据域和指针域(2个)进行初始化
    New->data = data;
    New->prev = NULL;
    New->next = NULL;

    return New;
    }
    /**

  • @function name: DoubleLList_NewNode

  • @brief 在列表头部插入新结点

  • @param 介绍函数参数 @head @data

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    // 头插
    bool DoubleLList_t_HeadInsert(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.如果链表为非空,则把新结点插入到链表的头部
    New->next = Head->next;
    Head->next->prev = New;
    Head->next = New;

    return true;
    }
    // 尾插
    /**

  • @function name: DoubleLList_t_TailInsert

  • @brief 在列表尾部插入新结点

  • @param 介绍函数参数 @head @data

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    bool DoubleLList_t_TailInsert(DoubleLList_t *Head, DataType_t data)
    {
    // 1.创建新的结点,并对新结点进行初始化
    DoubleLList_t *Phead = Head;
    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;
    }
    while (Phead)
    {
    Phead = Phead->next;
    }
    Phead->next = New;
    New->prev = Phead;
    }

// 中间位置插入
/**

  • @function name: DoubleLList_t_DestInsert

  • @brief 在列表任意部插入新结点

  • @param 介绍函数参数 @head @data @dest

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    bool DoubleLList_t_DestInsert(DoubleLList_t *Head, DataType_t dest, DataType_t data)
    {
    // 1.创建新的结点,并对新结点进行初始化
    DoubleLList_t *Phead = Head;
    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;
    }
    while (Phead != dest)
    {
    Phead = Phead->next;
    }
    New->next = Phead->next;
    Phead->next->prev = New;
    Phead->next = New;
    New->prev = Phead;
    return true;
    }

// 头删
/**

  • @function name: DoubleLList_t_HeadDel

  • @brief 删除首结点

  • @param 介绍函数参数 @head @data

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    bool DoubleLList_t_HeadDel(DoubleLList_t Head)
    {
    // 1.创建新的结点,并对新结点进行初始化
    DoubleLList_t Phead = Head->next;
    if (Head->next == NULL)
    {
    printf("list is empty , can not delate");
    return false;
    }
    Head->next = Head->next->next;
    Phead->next = NULL;
    Head->next->prev = NULL;
    free(Phead);
    return false;
    }
    // 尾删
    /

  • @function name: DoubleLList_t_TailDel

  • @brief 删除尾结点

  • @param 介绍函数参数 @head @data

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    bool DoubleLList_t_TailDel(DoubleLList_t *Head)
    {
    // 1.创建新的结点,并对新结点进行初始化
    DoubleLList_t *Phead = Head;
    if (Head->next == NULL)
    {
    printf("list is empty , can not delate");
    return false;
    }
    while (Phead)
    {
    Phead = Phead->next;
    }
    Phead->prev->next = NULL;
    Phead->prev = NULL;
    free(Phead);

    return false;
    }

/**

  • @function name: DoubleLList_t_DestDel

  • @brief 删除指定位置结点

  • @param 介绍函数参数 @head @data @dest

  • @retval bool

  • @date 2024/04/23

  • @version 1.0 :版本

  • @note 补充 注意 说明
    */
    bool DoubleLList_t_DestDel(DoubleLList_t *Head, DataType_t dest)
    {
    // 1.创建新的结点,并对新结点进行初始化
    DoubleLList_t *Phead1 = Head;
    DoubleLList_t *Phead2 = Head;
    if (Head->next == NULL)
    {
    printf("list is empty , can not delate");
    return false;
    }
    while (Phead1)
    {

     Phead1 = Phead1->next;
    

    }
    if (Phead1->data != dest)
    {
    return false;
    }
    while (Phead2->data != dest)
    {
    Phead2 = Phead2->next;
    }
    Phead2->prev->next = Phead2->next;
    Phead2->next->prev = Phead2->prev;
    Phead2->prev = NULL;
    Phead2->next = NULL;
    return true;
    }
    int main(int argc, char const *argv[])
    {

    return 0;
    }

posted @ 2024-04-23 21:18  Zeratul$$$  阅读(2)  评论(0编辑  收藏  举报