数据结构题典016:按递增次序输出单链表所有元素(ANSI C)

题目:按递增次序输出单链表所有元素,并释放结点的存储空间。要求不使用数组做辅助空间。

#include <limits.h>

void min_elem( link_list * lst, node_ptr * min_prev, node_ptr * min_ptr )
{
	node_ptr p = (*lst)->next, prev = NULL;
	elem_type min_v = INT_MAX;
	prev = *lst;
	while( p != NULL )
	{
		if( p->data < min_v )
		{
			min_v = p->data;
			*min_ptr = p;
			*min_prev = prev;
		}
		prev = p;
		p = p->next;
	}
}

void print_ascend_order_llist( link_list lst )
{
	node_ptr p = lst, prev = NULL, min_prev = NULL, min_ptr = NULL;
	elem_type min_v;

	while( lst->next != NULL )
	{
		min_elem( &lst, &min_prev, &min_ptr );
		printf( "%d ", min_ptr->data );
		min_prev->next = min_ptr->next;
		free( min_ptr );
	}
	putchar( '\n' );
}

你可能感兴趣的:(数据结构,c,list,null,存储)