del

删除链表的任意位置

//删除任意位置
bool LLsit_Del(LList_t *Head,int dest)
{

	int num = 0;
    //记录头结点和首结点
	LList_t *Phead = Head;
	LList_t *Pnext = Head->next;
	//1.判断链表是否为空,如果为空,则直接插入即可
	if (NULL == Head->next){
		return false;
	}
    //遍历链表
	while(Phead->next){
		//判断当前位置是否是想要删除的位置
		if(dest == num++){
            //删除
			Phead->next = Pnext->next;
			Pnext->next = NULL;
            //释放内存
			free(Pnext);
			return true;
		}
        //移动
		Phead = Phead->next;
		Pnext = Pnext->next;
	}
    //遍历完链表,没有返回,则证明dest不在该链表的返回
	return false;
}

设计一个算法删除单链表L(有头结点)中的一个最小值结点

//删除最小值
bool DelMin(LList_t *Head)
{
	//对链表的头文件的地址进行备份
	LList_t *Phead = Head;
    //记录链表的第一个值
	int min = Phead->next->data;
	int i = 0;
	int cnt = 0;
	//首结点
	while(Phead->next){
        //移动链表
		Phead = Phead->next;
        //判断min是不是最小值
		if(min > Phead->data){
			min = Phead->data;
			cnt = i;
		}
		i++;
	}
	return LLsit_Del(Head,cnt);
}
posted @ 2024-04-22 21:59  歪币八卜  阅读(32)  评论(0)    收藏  举报