双向不循环链表

双向不循环链表

/********************************************************************************************************
 *
 *
 * 设计双向链表的接口
 *
 *
 *
 * Copyright (c)  2023-2024   cececlmx@126.com   All right Reserved
 * ******************************************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>

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

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

} DoubleLList_t;
/**
 *
 *  name     :  DoubleLList_Create
 *  function :  创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化
 *  argument :  None
 *  retval   :  None
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *
 *  /
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;
}


/**
 *
 *  name     :  DoubleLList_NewNode
 *  function :  创建新的结点,并对新结点进行初始化(数据域 + 指针域)
 *  argument :  
 *              @data:数据
 *  retval   :  新结点的地址
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *
 *  /

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;
}


/**
 *  name     :  DoubleLList_HeadInsert
 *  function :  头部插入结点
 *  argument :  
 *              @Head:头结点
 *              @data:数据
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
bool DoubleLList_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;
    }

    // 如果链表为空
    if (NULL == Head->next)
    {
        // 头结点指向新结点
        Head->next = New;
        return true;
    }

    // 把新结点指向首结点
    New->next = Head->next;

    // 把首结点指向新结点
    Head->next->prev = New;

    // 把首结点指向结点
    Head->next = New;

    return true;
}


/**
 *  name     :  DoubleLList_HeadInsert
 *  function :  尾部插入结点
 *  argument :  
 *              @Head:头结点
 *              @data:数据
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
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;
    }
    // 如果链表为空
    if (NULL == Head->next)
    {
        // 头结点指向新结点
        Head->next = New;
        return true;
    }

    DoubleLList_t *temp = Head; // 记录尾结点
    // 遍历链表,找到尾结点
    while (temp->next)
    {
        temp = temp->next;
    }

    // 把尾结点指向新结点
    temp->next = New;
    // 把新结点指向尾结点
    New->prev = temp;
    return true;
}

/**
 *  name     :  DoubleLList_HeadInsert
 *  function :  指定位置插入结点
 *  argument :  
 *              @Head:头结点
 *              @data:数据
 *              @Destval:指定数据
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
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;
    }
    // 如果链表为空
    if (NULL == Head->next)
    {
        // 头结点指向新结点
        Head->next = New;
        return true;
    }

    DoubleLList_t *Phead = Head;
    // 遍历链表,找到插入位置 qq
    while (Phead->next) // 0  1
    {
        Phead = Phead->next;
        if (Destval == Phead->data)
        {
            break;
        }
    }
    // 如果找到了目标结点

    // 把新结点指向目标结点的直接后驱
    New->next = Phead->next;
    // 把目标结点的直接后驱指向新结点
    Phead->next->prev = New;
    // 把新结点指向目标结点
    New->prev = Phead;
    // 把新结点指向目标结点
    Phead->next = New;
}
/**
 *  name     :  DoubleLList_Print
 *  function :  遍历链表
 *  argument :  
 *              @Head:头结点
 *              
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
// 遍历链表
bool DoubleLList_Print(DoubleLList_t *Head)
{
    // 对单向循环链表的头结点的地址进行备份
    DoubleLList_t *Phead = Head;

    // 判断当前链表是否为空,为空则直接退出
    if (Head->next == Head)
    {
        printf("current linkeflist is empty!\n");
        return false;
    }

    // 从首结点开始遍历
    while (Phead->next)
    {
        // 把头结点的直接后继作为新的头结点
        Phead = Phead->next;

        // 输出头结点的直接后继的数据域
        printf("data = %d\n", Phead->data);
        usleep(100000);

        // 判断是否到达尾结点,尾结点的next指针是指向首结点的地址
        if (Phead->next == Head->next)
        {
            break;
        }
    }

    return true;
}

/**
 *  name     :  DoubleLList_Print
 *  function :  头部删除结点
 *  argument :  
 *              @Head:头结点
 *              
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
    // 判断链表是否为空
    if (NULL == Head->next)
    {
        return false;
    }
    // 对链表首结点进行备份
    DoubleLList_t *Phead = Head->next;
    // 链表为非空则删除结点
    Head->next = Head->next->next;
    Phead->next = NULL;
    Head->next->next->prev = NULL;
    free(Phead);
    return true;
}


/**
 *  name     :  DoubleLList_Print
 *  function :  尾部删除结点
 *  argument :  
 *              @Head:头结点
 *              
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
bool DoubleLList_TailDel(DoubleLList_t *Head)
{
    // 判断链表是否为空
    if (NULL == Head->next)
    {
        return false;
    }
    // 用于存储尾结点
    DoubleLList_t *Phead = Head;
    // 用于遍历找到尾结点
    while (Phead->next)
    {
        Phead = Phead->next;
    }

    // 链表为非空则删除结点
    Phead->prev->next = NULL;
    Phead->prev = NULL;
    free(Phead);
    return true;
}

/**
 *  name     :  DoubleLList_Print
 *  function :  指定位置删除删除结点
 *  argument :  
 *              @Head:头结点
 *              @Desyval:指定结点的数据值
 *  retval   :  false and true
 *  author   :  yanxianwei@163.com
 *  date     :  2024/04/17
 *  note     :  None
 *  /
 
bool DoubleLList_DestDel(DoubleLList_t *Head, DataType_t Destval)
{
    // 判断链表是否为空
    if (NULL == Head->next)
    {
        return false;
    }

    // 定义一个指针存储待删除的指定结点
    DoubleLList_t *dest = Head;
    // 遍历链表,找到待删除接结点
    while (dest->next)
    {
        dest = dest->next;
        if (Destval == dest->data)
        {
            break;
        }
    }

    if (NULL == dest)
    {
        return false;
    }
    // 链表为非空则删除结点
    dest->prev->next = dest->next;
    dest->next->prev = dest->prev;
    dest->prev = NULL;
    dest->next = NULL;
    free(dest);
    return true;
}

int main(int argc, char const *argv[])
{
    DoubleLList_t *Head = DoubleLList_Create();
    
    return 0;
}
posted @   Yxw_lp  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示