数据元素的位置进行查找,代码如下:int search_seq_list(list_t *seq_list,int pos,int *num){
if(NULL == seq_list || NULL == num){
printf("内存分配失败\n");
return -1;
}
if( pos < 0 || pos >= seq_list->count){
printf("查找位置不合理,查找失败\n");
return -1;
}
*num = seq_list->a[pos].num;
return 0;
}
入参合理性检查,比如函数中的形参seq_list和num;if条件语句内容和任意位置删除功能函数一致;数据元素的大小,对数据元素做冒泡排序,代码如下:int sort_seq_list(list_t *seq_list){
if(NULL == seq_list){
printf("入参为NULL\n");
return -1;
}
for(int i = 0 ; i < seq_list->count - 1; i++){
for(int j = 0; j < seq_list->count - 1 - i; j++ ){
if(seq_list->a[j].num > seq_list->a[j+1].num){
data_t temp = seq_list->a[j];
seq_list->a[j] = seq_list->a[j+1];
seq_list->a[j+1] = temp;
}
}
}
printf("本次排序结束\n");
return 0;
}
结构体变量,作为第三方变量,根据if条件语句,交换两个数据元素的位置;第二层for循环中j < seq_list->count - 1 - i;的-1是为了防止越界;剔除顺序表中的重复的数据元素,例如:11 33 55 55 66 66 66 66
本次去重结束
11 33 55 66
int del_rep_seq_list(list_t *seq_list){
if(NULL == seq_list){
printf("入参为NULL\n");
return -1;
}
for(int i = 0; i < seq_list->count; i++){
for(int j = i + 1; j < seq_list->count;){
if(seq_list->a[i].num == seq_list->a[j].num){
for(int k = j; k < seq_list->count-1; k++){
seq_list->a[k] = seq_list->a[k+1];
}
seq_list->count--;
} else {
j++;
}
}
}
printf("本次去重结束\n");
return 0;
}
第二层for循环是为了在顺序表中寻找相同的数据元素;去重的具体操作方法和在顺序表的任意位置删除数据元素的操作一致;