首页 > 编程语言 >实现一个算法删除单链表L(有头结点)中的一个最小值结点

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

时间:2024-04-23 11:27:11浏览次数:17  
标签:Node 结点 单链 head 有头 LList 最小值 next 节点

/********************************************************************************************************
*
*	file name:	Zqh_splist_4.22.3.c
* 	author	 :	[email protected]
* 	date	 :	2024/04/23
* 	function :	设计一个算法删除单链表L(有头结点)中的一个最小值结点	
*	note	 :	考研题
*	
*  Copyright (c)  2023-2025   [email protected]    All right Reserved
* ******************************************************************************************************/

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>


typedef int DataType_t;

typedef struct LinkedList
{
	DetaType_t			data;
	struct LinkedList	*next;	
};LList_t

//删除单链表中最小值的节点(有头节点的)
void deleteMinNode(LList_t* head)
{
	//判断当前链表是否为空的情况
	if(NULL == head->next){
		printf("链表为空\n");
		return;
	}
	
	LList_t* Last == head ; //当前节点的上一级节点,目前指向头节点
	
	LList_t* Node = head->next;//当前节点,目前指向首节点

	LList_t* minNode = head ->next;//保存最小值的节点

	LList_t* minPrev = head; //保存最小值的上一个节点,方便连接


	//循环的查找最小值并保存下来
	while(Node != NULL){
		if (Node->data < minNode->data ){
			minNode =Node;  
			minPrev = Last; 
		}
	Last = Node;
	Node = Node ->next; 

	}

	//删除最小值 
	if ( head == minPrev )
		head->next = minNode->next;   //删除最小值节点是第一个

	 Last->next = minNode->next;  //连接
	 free(minNode);

}

标签:Node,结点,单链,head,有头,LList,最小值,next,节点
From: https://www.cnblogs.com/kencszqh/p/18152409

相关文章