单链表的头插、尾插、中间插、头删、尾删、中间删

//指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
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)
{
    LList_t *Phead = Head;
        
    //1.创建新的结点,并对新节点进行初始化
    LList_t *New = LList_NewNode(data);
    if(NULL == New)
    {
         printf("can not inser new node\n");
         return false;
     }
     //2.判断链表是否为空,如果为空,则直接插入即可
     if(NULL == Head->next)
     {
         Head->next = New;
         return ture;
     }
     //3.如果链表为非空,则把新结点插入到链表的尾部
     while(Phead->next)
     {
         Phead = Phead->next  //直接用风险太高了,定义一个Phead用
     }
     Phead->next = New;
     return ture;
}
//指定位置插入
bool LList_DestInsert(LList_t *Head,DataType_t destval,DataType_t data)
{
     LList_t *Phead = Head->next;
        
    //1.创建新的结点,并对新节点进行初始化
    LList_t *New = LList_NewNode(data);
    if(NULL == New)
    {
         printf("can not inser new node\n");
         return false;
     }
     //2.判断链表是否为空,如果为空,则直接插入即可
     if(NULL == Head->next)
     {
         Head->next = New;
         return ture;
     }
     //3.(加前面还是加后面?递增还是递减,没有特殊需求可以不用设计)、
     //遍历链表,目的找目标结点,比较结点的数据域
    while(Phead !=NULL && destval != Phead->data)
    {
        Phead = Phead->next;
    }  
    if(NULL == Phead)
    {
        return false;
    }
    //4.说明找到目标结点,则把目标结点加入到目标的后面
    New->next = Phead->next;
    Phead->next = New;
    
    return ture;
}

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

		//输出头结点的直接后继的数据域
		printf("data = %d\n",Phead->data);
	}
    
}
//头删
bool LList_HeadDel(LList_t *Head)
{
    //对链表的头文件的地址进行备份
	LList_t *Phead = Head;
    
    //2.判断链表是否为空,如果为空,则直接退出
    if(NULL == Head->next)
    {
        return false;
    }
    //3.链表是非空的,则直接删除首结点
    //循环用Phead,用Head会有风险
    Head->next = Phead->next->next;
    Phead->next->next = NULL;
    free(Phead->next);
    
    return true;    
}
//尾删 -- myself
bool LList_HeadDel(LList_t *Head)
{
    //对链表的头文件的地址进行备份
	LList_t *Phead = Head;
    
    //2.判断链表是否为空,如果为空,则直接退出
    if(NULL == Head->next)
    {
        return false;
    }
    //3.如果链表为非空,则遍历到尾结点,删除尾结点
    while(Phead->next)
    {
        Phead = Phead->next  //直接用风险太高了,定义一个Phead用
    }
    free(Phead);
    
    return ture;    
}
posted @ 2024-04-25 21:09  小懿同学  阅读(85)  评论(0)    收藏  举报