单项链表的一些基础操作

/**********************************************
*file name:LinkList.c
*author:lyn2736001522@163.com
date:2024/4/2
function:设计顺序表
note:None
CopyRight (c) 2023-2024 邮箱 All Right Reseverd
/
//指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
//构造链表的节点,节点链表中所有的数据类型都一样
typedef struct LinkedList
{
DataType_t data; //节点的数据域
struct LinkedList * next;
}LList_t;
/

*

  • function name:max
  • function functionality:创建一个空链表
  • function list:
  •             @a:void
    
  •             @b:void
    
  • retuen value: LList_t *
  • author:lyn2736001522@163.com
  • date:2024/4/2
  • 修改历史:
  • 版本修改:
    1.v1.0.n(1表示主板本,0表示)
    2.
    3.
    ······
    ***/
    //创建一个空链表,头链表里应该有一个头节点,对链表进行初始化
    LList_t * LList_Create(void)
    {
    //1.创建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;
    }
    /
  • function name:max
  • function functionality:创建新结点
  • function list:
  •             @a:DataType_t data
    
  •             @b:void
    
  • retuen value: LList_t *
  • author:lyn2736001522@163.com
  • date:2024/4/2
  • 修改历史:
  • 版本修改:
    1.v1.0.n(1表示主板本,0表示)
    2.
    3.
    ······
    *****************************************************/

//创建新的结点,并对新结点进行初始化(数据域+指针域)
LList_t LList_Newnode(DataType_t data)
{
//1.创建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;
}
/
*************************************************
*

  • function name:max
  • function functionality:分别使用头插法、尾插法、指定插入三种方法插入新结点
  • function list:
  •             @a:LList_t * Head
    
  •             @b:DataType_t data
    
  • retuen value: LList_t *
  • author:lyn2736001522@163.com
  • date:2024/4/22
  • 修改历史:
  • 版本修改:
    1.v1.0.n(1表示主板本,0表示)
    2.
    3.
    ······
    *****************************************************/

//头插法
bool LList_HeadInsert(LList_t * Head ,DataType_t data)
{
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
LList_t *LList_Newnode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
return ture;
}
//3.如果链表非空则把新结点插入到链表的头部
New->next = head->next;
head->next = New;
return true;
}

//尾插法
bool LList_TailInsert(LList_t * Head ,DataType_t data)
{
LList_t *Phead = Head;
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
LList_t *LList_Newnode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
return ture;
}
//3.如果链表非空则把新结点插入到链表的尾部
while(Phead->next)
{
Phead = Phead->next;
}
Phead->next = New;
printf("nenode Insert success!");
return true;
}

//指定位置插入
bool LList_TailInsert(LList_t * Head ,DataType_t destval,DataType_t data)
{
LList_t *Phead = Head->next;
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
LList_t *LList_Newnode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
return ture;
}
//3.遍历链表,目的是找到目标结点,比较结点的数据域
while(Phead != NULL && destval !=Phead->data)
{
//把当前结点的直接后继作为新的当前结点
Phead = Phead->next;
}
if(Phead == NULL)
{
return false;
}
//到这一步说明找到了目标结点,则把目标结点加入到目标的后面
New->next = Phead->next;
Phead->next = New;
}

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

  • function name:max
  • function functionality:删除结点
  • function list:
  •             @a: LList_t * Head 
    
  •             @b: None
    
  • retuen value: LList_t *
  • author:lyn2736001522@163.com
  • date:2024/4/2
  • 修改历史:
  • 版本修改:
    1.v1.0.n(1表示主板本,0表示)
    2.
    3.
    ······
    *****************************************************/
    //头删
    bool LList_HeadDel(LList_t * Head)
    {
    //1.备份头结点的地址
    LList_t *Phead = Head;
    //判断单链表是否为空
    if(NULL == Head->next )
    {
    printf("当前列表为空,删除失败!");
    return false;
    }
    //链表是非空的,则直接删除首结点
    Head->next = Phead->next->next;
    Phead->next->next = NULL;
    free( Phead->next);
    printf("HeadNode Delete success!");
    return ture;
    }
posted @ 2024-04-24 08:49  小楠同志  阅读(1)  评论(0编辑  收藏  举报