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

来源:互联网 发布:开源大数据调度系统 编辑:程序博客网 时间:2024/04/30 02:24

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;}}
原创粉丝点击