C语言实现链表增删减改

/*******************************************************************
*

  • file name: main.c
  • author : 1810866453@163.com
  • date : 2024/04/22
  • function : 实现对链表的增删改查
  • note : None
  • CopyRight (c) 2023-2024 1810866453@163.com All Right Reseverd
  • *****************************************************************/

include <stdio.h>

include <stdbool.h>

include <stdlib.h>

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

// 构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct LinkedList
{
DataType_t data; // 结点的数据域
struct LinkedList *next; // 结点的指针域

} LList_t;

/*******************************************************************
*

  • 函数名称: LList_Create

  • 函数功能: 输出两个整数中较大的整数

  • 函数参数: void

  • 返回结果: 创建链表地址

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    // 创建一个空链表,空链表应该有一个头结点,对链表进行初始化
    LList_t *LList_Create(void)
    {
    // 1.创建一个头结点并对头结点申请内存
    LList_t *Head = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == Head)
    {
    perror("Calloc memory for Head is Failed");
    exit(-1);
    }

    // 2.对头结点进行初始化,头结点是不存储有效内容的!!!
    Head->next = NULL;

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

/*******************************************************************
*

  • 函数名称: LList_NewNode

  • 函数功能: 创建新的结点,并对新结点进行初始化(数据域 + 指针域)

  • 函数参数: data

  • 返回结果: 新节点地址

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    LList_t *LList_NewNode(DataType_t data)
    {
    // 1.创建一个新结点并对新结点申请内存
    LList_t *New = (LList_t *)calloc(1, sizeof(LList_t));
    if (NULL == New)
    {
    perror("Calloc memory for NewNode is Failed");
    return NULL;
    }

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

    return New;
    }

/*******************************************************************
*

  • 函数名称: LList_HeadInsert

  • 函数功能: 实现对链表头部插入

  • 函数参数: data

  • 返回结果: 插入成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    bool LList_HeadInsert(LList_t *Head, DataType_t data)
    {
    // 1.创建新的结点,并对新结点进行初始化
    LList_t *New = LList_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 = New;

    return true;
    }
    /*******************************************************************

  • 函数名称: LList_TailInsert

  • 函数功能: 实现对链表尾部插入

  • 函数参数: LList_t *Head, DataType_t data

  • 返回结果: 插入成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • */
    bool LList_TailInsert(LList_t Head, DataType_t data)
    {
    LList_t New = LList_NewNode(data);
    if (NULL == New)
    {
    printf("can not insert new node\n");
    return false;
    }
    LList_t Phead = Head;
    while (Phead->next)
    {
    Phead = Phead->next;
    }
    Phead->next = New;
    New->next = NULL;
    return true;
    }
    /

  • 函数名称: LList_DestInsert

  • 函数功能: 实现对链表任意位置插入

  • 函数参数: LList_t *Head, DataType_t dest, DataType_t data

  • 返回结果: 插入成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    bool LList_DestInsert(LList_t *Head, DataType_t dest, DataType_t data)
    {
    LList_t *New = LList_NewNode(data);
    if (NULL == New)
    {
    printf("can not insert new node\n");
    return false;
    }

    LList_t *Phead1 = Head;
    LList_t *Phead2 = Head;
    for (int i = 0; i < dest; i++)
    {
    Phead1 = Phead1->next;
    }
    for (int j = 0; j <= dest; j++)
    {
    New->next = Phead2;
    Phead1->next = New;
    }
    Phead2->next = New;
    }

/*******************************************************************
*

  • 函数名称: LList_Print

  • 函数功能: 遍历链表输出

  • 函数参数: LList_t *Head

  • 返回结果: 插入成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    void LList_Print(LList_t *Head)
    {
    // 对链表的头文件的地址进行备份
    LList_t *Phead = Head;

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

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

    }
    }

/*******************************************************************
*

  • 函数名称: LList_HeadDelate
  • 函数功能: 链表头部删除
  • 函数参数: LList_t *Head, DataType_t data
  • 返回结果: 删除成功或否
  • 注意事项: None
  • 函数作者: 1810866453@163.com
  • 创建日期: 2024/04/22
  • 修改历史:
  • 函数版本: V1.0
  • *****************************************************************/
    bool LList_HeadDelate(LList_t *Head, DataType_t data)
    {
    LList_t *Phead = Head->next;
    if (NULL == Head->next)
    {
    return false;
    }
    Head->next = Head->next->next;
    Head->next->next = NULL;
    free(Phead);
    }

/*******************************************************************
*

  • 函数名称: LList_TailDelate

  • 函数功能: 链表尾部删除

  • 函数参数: LList_t *Head, DataType_t data

  • 返回结果: 删除成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    bool LList_TailDelate(LList_t *Head, DataType_t data)
    {
    // 判断链表是否为空
    if (NULL == Head->next)
    {
    return false;
    }
    // 对链表的头文件的地址进行备份
    LList_t *Phead1 = Head;
    LList_t *Phead2 = Head;

    // 首结点开始进行遍历
    int count = 0;
    while (Phead1->next)
    {
    Phead1 = Phead1->next;
    // 计数器
    count++;
    }
    // 通过计数器遍历到倒数第二个结点的为止
    for (int i = 0; i < count - 1; i++)
    {
    Phead2 = Phead2->next;
    }

    Phead2->next = NULL;
    // 释放所删除结点占用空间
    free(Phead1);
    return true;
    }
    /*******************************************************************

  • 函数名称: LList_DestDelate

  • 函数功能: 链表任意位置删除

  • 函数参数: LList_t *Head, DataType_t data

  • 返回结果: 删除成功或否

  • 注意事项: None

  • 函数作者: 1810866453@163.com

  • 创建日期: 2024/04/22

  • 修改历史:

  • 函数版本: V1.0

  • *****************************************************************/
    bool LList_DestDelate(LList_t *Head, DataType_t data)
    {
    LList_t *Phead1 = Head->next;
    LList_t *Phead2 = Head->next;
    int count = 0;
    // 判断链表是否为空
    if (NULL == Head)
    {
    perror("Calloc memory for Head is Failed");
    exit(-1);
    }
    // 第一次遍历找到data所在位置
    while (Phead1)
    {
    Phead1 = Phead1->next;
    // 用计数器记录遍历次数
    count++;
    if (Phead1->data == data)
    break;
    }
    // 通过计数器使第二次遍历到所删结点的直接前驱结点
    for (int i = 0; i < count - 1; i++)
    {
    Phead2 = Phead2->next;
    }
    Phead2->next = Phead1->next;
    free(Phead1);
    return true;
    }

int main(int argc, char const *argv[])
{

return 0;

}

posted @ 2024-04-22 20:22  Zeratul$$$  阅读(23)  评论(0)    收藏  举报