首页 > 其他分享 >单向链表遍历插入和删除

单向链表遍历插入和删除

时间:2024-04-22 21:33:25浏览次数:14  
标签:head 遍历 单向 结点 next 链表 LList NULL

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

  •      file name:  002_单向链表.c
    
  •      author   :  [email protected]
    
  •      date     :  2024/04/22
    
  •      function :  单向链表的遍历插入和删除功能的完善
    
  •      note     :  None
    
  •      CopyRight (c) 2024-2024  [email protected]    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_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_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;
}

//头插
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;
}
//尾插
bool LList_TailInsert(LList_t *Head,DataType_t data)
{
    //1.创建新的结点,并对新结点进行初始化
    LList_t *New = LList_NewNode(data);
    LList_t *tmp = Head;
    while (tmp->next)
    {
        tmp = tmp->next;
    }
    //将新节点的后继节点指向NULL,尾节点的后继指针指向新指针
    New->next = NULL;
    tmp->next = New;
    return true;
    
}

//找到某个特定的位置做插入
bool LList_DestInsert(LList_t *Head,DataType_t dest,DataType_t data)
{
    //创建新的节点,初始化新数据
    LList_t *New = LList_NewNode(data);
    //定义一个临时指针变量存储首节点
    LList_t *tmp = Head;
    //定义一个变量i,充当下标查找节点
    int i = 0;
    //当下标在插入目标位置跳出循环
    while (i != dest)
    {
        //下标在前一个位置跳出循环,所以tmp当前位置在目标节点的直接前继节点
        tmp = tmp->next;
        //为了遍历到目标位置,所以i自加让循环遍历到目标位置
        i++;
        //如果传进来的下标大于链表的长度,输入这句话并返回false
        if (tmp->next == NULL)
        {
            printf("需要插入的位置大于链表的长度了!\n");
            return false;
        }
    }
    //插入到指定位置操作
    New->next = tmp->next;
    tmp->next = New;
    return true;
}
//头删
bool HeadDel(LList_t *head)
{
    if (head->next == NULL)
    {
        printf("此链表为空!做不了删除动作!\n");
        return false;
    }
    //定义一个变量存储首节点
    LList_t *tmp = head->next;
    head->next = tmp->next;
    tmp->next = NULL;
    free(tmp);
    return true;
}
//尾删
void TailDel(LList_t *head)
{
    if (head->next == NULL)
    {
        printf("此链表为空!做不了删除动作!\n");
        return;
    }
    //定义一个变量存储首节点
    LList_t *tmp1 = head;
    //当tmp1(倒数第二个节点)的下下个节点指向NULL时,就说明tmp1的下个节点就是尾节点,需要删除的节点
    while (tmp1->next->next != NULL)
    {
        tmp1 = tmp1->next;
    }
    //释放掉尾节点
    free(tmp1->next);
    tmp1->next = NULL;
    
    
}
//通过下标删除节点
bool DelNode_index(LList_t *head,DataType_t dest)
{
    if (head->next == NULL)
    {
        printf("此链表为空!做不了删除动作!\n");
        return false;
    }
    //定义两个指针变量存储头节点和首节点
    LList_t *tmp1 = head;
    LList_t *tmp2 = head->next;
    //定义一个下标,方便后面查找
    int i = 1;
    //当下标不等于目标下表时进入循环
    while (i != dest)
    {
        //向后遍历,然后一遍一遍判断
        tmp1 = tmp2;
        tmp2 = tmp2->next;
        i++;
        
        if (tmp2->next == NULL)
        {
            printf("已经到最后一个元素了,没有找到需要删除的元素\n");
            return false;
        }
    }
    tmp1->next = tmp2->next;
    tmp2->next = NULL;
    free(tmp2);
    return true;
}

// //通过数据删除节点
bool DelNode_Data(LList_t *head,DataType_t data)
{
    LList_t *tmp1 = head;
    LList_t *tmp2 = head->next;
    //判断条件改成对比数据是否相等,不相等进去循环,往后遍历,相等跳出循环,删掉数据
    while(tmp2->data != data)
    {
        tmp1 = tmp2;
        tmp2 = tmp2->next;
        //判断链表中是否有匹配的数据,没有打印printf("链表中没有你要删除的数据!\n")
        if (tmp2->next == NULL)
        {
            printf("链表中没有你要删除的数据!\n");
            return false;
        }
        
    }
    tmp1->next = tmp2->next;
    tmp2->next = NULL;
    free(tmp2);
    return true;
    

}


//遍历
void LList_Print(LList_t *Head)
{
	//对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	
	//首结点
	while(Phead->next)
	{
		//把头的直接后继作为新的头结点
		Phead = Phead->next;

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

}




int main(int argc, char const *argv[])
{
    //创建了一个空链表,定义了head指针变量接受返回值头节点
    LList_t *head = LList_Create();
    //头插插入链表
    LList_HeadInsert(head,1);
    LList_HeadInsert(head,2);
    LList_HeadInsert(head,3);
    LList_HeadInsert(head,4);
    LList_HeadInsert(head,5);
    LList_HeadInsert(head,6);
    LList_Print(head);
    printf("\n");
    //尾插
    printf("尾插发:\n");
    LList_TailInsert(head,10);
    LList_TailInsert(head,100);
    LList_Print(head);
    printf("\n");
    //向某个位置插入
    printf("向某个位置插入:\n");
    LList_DestInsert(head,4,255);
    LList_DestInsert(head,6,1000);
    // LList_DestInsert(head,15,255);
    LList_Print(head);
    printf("删除完后的链表:\n");
    // DelNode(head,7);
    // DelNode(head,15);
    HeadDel(head);
    TailDel(head);
    DelNode_Data(head,1000);
    LList_Print(head);
	return 0;
}

标签:head,遍历,单向,结点,next,链表,LList,NULL
From: https://www.cnblogs.com/wwwwariana/p/18151595

相关文章

  • 删除链表中指定结点
    删除链表中指定结点/********************************************************name:DelTargetNode* function:删除链表中指定的结点*argument*@head:链表头结点的地址*@num:需要删除的第num个结点**retva......
  • 设计一个算法删除单链表L(有头节点)中的一个最小值结点
    数据结构链表笔试题:设计一个算法删除单链表L(有头节点)中的一个最小值结点。/****************************************************************** * filename : linkedlist.c* author : [email protected]* data : 2024/04/22* function : 删除单链表中的一个最小......
  • 链表
    链表在C语言中,链表是一种常用的数据结构,它可以用来存储一系列的元素。链表中的每个元素都存储了下一个元素的地址,从而形成了一条链。这种结构使得在插入和删除元素时不需要像数组那样移动大量元素,因此它在插入和删除操作多的情况下有很大的优势。在C语言中,链表可以有多种实现方......
  • 设计一个算法删除单链表L(有头结点)中的一个最小值结点
    设计一个算法删除单链表L(有头结点)中的一个最小值结点/********************************************************************* 函数名称: LList_delatemin* 函数功能:删除单链表L(有头结点)中的一个最小值结点* 函数参数:* @a:*L*@b:*返回......
  • 假设该链表只给出了头指针 head。在不改变链表的前提下,请设计一个尽可能高效的算法,查
    假设该链表只给出了头指针head。在不改变链表的前提下,请设计一个尽可能高效的算法,查找链表中倒数第k(k为正整数)个位置上的结点。若查找成功,算法输出该结点的data值,并返回1;否则,只返回0。/********************************************************************* 函数名称......
  • 查找链表中倒数第k个数
    include<stdio.h>include<stdbool.h>include<stdlib.h>/********************************************************************函数名称: MinDelate函数功能:/*假设该链表只给出了头指针head。在不改变链表的前提下,请设计一个尽可能高效的算法,查找链表中倒数第k(......
  • C语言单向链表的创建和增删减查相关程序
    对单向链表进行删除和插入的程序设计/********************************************************************* filename: linkedlist.c* author :[email protected]* date :2024/04/22* function:实现单向链表的创建、拆入、删除功能* note :None**......
  • 删除链表中最小的一个结点
    include<stdio.h>include<stdbool.h>include<stdlib.h>//指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改typedefintDataType_t;//构造链表的结点,链表中所有结点的数据类型应该是相同的typedefstructLinkedList{DataType_tdata;//结点......
  • 删除链表中的尾部结点
    删除链表中的尾部结点/********************************************************name:DelTailNode* function:删除链表尾部结点*argument*@head:链表头结点的地址**retval:成功返回1,失败返回0*author:Dazz*......
  • 《渣男代码历险记》第五章:设计一个算法删除单链表L(有头结点)中的一个最小值结点
    为了删除单链表L中的一个最小值结点,我们可以遍历链表,找到最小值结点及其前驱结点,然后修改前驱结点的指针,使其指向最小值结点的下一个结点。以下是算法的解析和代码实现:初始化两个指针pre和cur,分别指向头结点和头结点的下一个结点。初始化一个变量min_val,用于存储当前最小值,将其......