首页 > 其他分享 >双向不循环链表

双向不循环链表

时间:2024-04-24 09:02:02浏览次数:17  
标签:结点 Head Phead DoubleLList next 链表 循环 双向

双向不循环链表

/********************************************************************************************************
 *
 *
 * 设计双向链表的接口
 *
 *
 *
 * Copyright (c)  2023-2024   [email protected]   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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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   :  [email protected]
 *  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;
}

标签:结点,Head,Phead,DoubleLList,next,链表,循环,双向
From: https://www.cnblogs.com/Yxwwant/p/18154265

相关文章

  • 单项链表的一些基础操作
    /***********************************************filename:LinkList.c*author:[email protected]:2024/4/2function:设计顺序表note:NoneCopyRight(c)2023-2024邮箱AllRightReseverd///指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改type......
  • 单项循环链表的一些基本操作
    //设计单向循环列表/***********************************************filename:circularlinkedlist.c*author:[email protected]*date:2024/4/23*function:设计单向循环列表*note:None*CopyRight(c)2023-2024邮箱AllRightReseverd**************************......
  • 双向循环链表的一些基础操作
    /***********************************************filename:DoubleList.c*function:设计双向链表*author:[email protected]*date:2024/4/23*note:None*CopyRight(c)2023-2024邮箱AllRightReseverd***********************************************///指的是......
  • 数据结构笔试题——基于C语言的链表功能函数实现
    题目1题目要求如下:/***@functionname:LList_CntdmFind*@brief查找链表中,倒数第k个位置上的节点*@param:​ @Head:链表头节点​ @k :倒数第k个位置*@retval:int型返回值;返回-1时即为失败,返回0时表示成功;*@date:2024/04/23*@version1.0*@n......
  • 单向与双向循环链表
    单向循环链表/********************************************************************* 函数名称: *函数功能:设计单向循环链表的接口*函数参数:* ​*返回结果:*注意事项:None*函数作者:zcx982795194@[email protected]*创建......
  • C语言单向循环链表的增删操作
    /***********************************************************************************************************设计双向链表的接口****Copyright(c) 2023-2024 [email protected] AllrightReserved****************************************......
  • 单向循环链表和双向链表程序编程
    链表学习记录设计单向循环链表的接口/***********************************************************************************************************设计单向循环链表的接口****Copyright(c)[email protected]*************......
  • 双向链表的接口的接口程序
    双向链表的接口的接口程序/********************************************************************* filename: 双向链表的接口的接口程序* author :[email protected]* date :2024-4-23* function:* note :None** CopyRight(c)20241764757......
  • 链表(考研算法)
    数据结构链表练习题:1.已知,一个带有头结点的单链表,结点结构为:假设该链表只给出了头指针head。在不改变链表的前提下,请设计一个尽可能高效的算法,查找链表中倒数第k(k为正整数)个位置上的结点。若查找成功,算法输出该结点的data值,并返回1;否则,只返回0。/**************************......
  • 双向循环链表
    小白感觉双向链表和单向链表的区别并不大,就是地址的交接有点繁琐,需要清晰的逻辑,简单理解就是俩条平行线,无线延伸,但是俩个线不交叉,但都是在一张纸上开始延展,头结点就像这张纸,理解的可能有点抽象,但我感觉这就是个抽象的概念,所以特编写初级的代码如下:/*****************************......