数据结构题典006:有序表中冗余元素的删除(ANSI C)

1、顺序表,假设元素已按升序排序

/*
 * remove redundant elements from ordered sequence
 */
int remove_redundant_elem( int c[], int n )
{
	int i = 0, j = 1;
	while( j < n )
	{
		if( c[i] != c[j] )
			c[++i] = c[j];
		++j;
	}
	return i + 1;
}

2、单链表,假设元素已按升序排序

void remove_redundant_elem_llist( link_list * lst )
{
	node_ptr h = *lst, r = NULL, p = NULL;
	assert( h != NULL );
	r = h->next;
	if( r != NULL )
		p = r->next;

	while( p != NULL )
	{
		if( p->data != r->data )
			r = p;
		else
		{
			r->next = p->next;
			free( p );
		}
		p = r->next;
	}
}

你可能感兴趣的:(数据结构题典006:有序表中冗余元素的删除(ANSI C))