C 从数组中删除结构
我正在尝试实现一个函数,该函数在一个排序的结构数组中循环,如果“key”(第一个字段值)有重复项,它将保留该key-value对的第一次迭代,并删除随后出现的任何重复项。这是我的密码:C 从数组中删除结构,c,struct,C,Struct,我正在尝试实现一个函数,该函数在一个排序的结构数组中循环,如果“key”(第一个字段值)有重复项,它将保留该key-value对的第一次迭代,并删除随后出现的任何重复项。这是我的密码: #include <stdlib.h> #include <stdio.h> struct Map * collect_values(int n, int *arr); void sort_values(struct Map *ptr, int n); void print(struct
#include <stdlib.h>
#include <stdio.h>
struct Map * collect_values(int n, int *arr);
void sort_values(struct Map *ptr, int n);
void print(struct Map *print_struct, int n);
struct Map * remove_duplicates(struct Map *ptr, int n);
struct Map{
int value, position;
};
int compare(const void *ptr1, const void *ptr2){
const struct Map *aptr = ptr1;
const struct Map *bptr = ptr2;
if(aptr->value == bptr->value){
return (aptr->position > bptr->position) -
(aptr->position < bptr->position);
}
else{
return (aptr->value > bptr->value) - (aptr->value < bptr->value);
}
}
int compare2(const void *aptr, const void *bptr){
int a = ((struct Map*)aptr)->position, b = ((struct
Map*)bptr)->position;
return (a > b) - (a < b);
}
int main(){
int size, i;
scanf("%d", &size);
int *arr = (int*) malloc(size*sizeof(int));
struct Map *p = collect_values(size,arr);
printf("Struct before sorting:\n");
print(p,size);
qsort(p,size,sizeof(struct Map),compare);
printf("Struct after sorting\n");
print(p,size);
struct Map *p2 = remove_duplicates(p,size);
printf("\nStruct after removing in the main\n");
for(i = 0; i < sizeof(*p2); i++){
printf("%d : %d\n", p2[i].value, p2[i].position);
}
free(p);
free(arr);
free(p2);
return 0;
}
struct Map * collect_values(int n, int *arr){
int i, position = 0;
struct Map *array = calloc(n,sizeof(*array));
for(i = 0; i < n; i++){
scanf("%d",&arr[i]);
array[i].value = arr[i];
array[i].position = position;
position++;
}
return array;
}
void print(struct Map * print_struct, int n){
int i;
for (i = 0; i < n; i++){
printf("%d : %d\n", print_struct[i].value, print_struct[i].position);
}
}
struct Map * remove_duplicates(struct Map *ptr, int n){
int i, j = 0, newsize;
struct Map *new_struct = calloc(n,sizeof(*new_struct));
new_struct[0] = ptr[0];
for(i = 1; i < n; i++){
if(ptr[j].value != ptr[i].value){
j++;
new_struct[j].value = ptr[i].value;
new_struct[j].position = ptr[i].position;
}
}
newsize = j+1;
//new_struct = realloc(new_struct, newsize);
printf("\nSorting in the function:\n");
for(i = 0; i < newsize; i++){
printf("%d : %d\n", new_struct[i].value, new_struct[i].position);
}
return new_struct;
}
但是,我得到了一个额外的键值5:5,因此不会被删除:
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
5 : 5
7 : 6
25 : 0
88 : 7
我试图手动循环,我认为我在使用j++时犯了一个错误,因为当条件为false(即存在重复项)时,我迭代,而j保留在后面,我认为这可能是我删除重复项4而不是重复项5的原因
我哪里做错了?我也对硬编码索引0处的值感到奇怪,但显然第一个值不是我要删除的值(只需要删除重复的后续值),而且因为我从1开始,所以不会对它进行比较
最后,remove_duplicate()函数返回一个struct*类型。当我从main打印时,我得到以下输出:
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
5 : 5
7 : 6
25 : 0
由于某些原因,最后一个值没有指向。我希望我的remove函数可以从main访问,因为我希望将指针p2传递给另一个按键值对中的值排序的函数。我的最后一个值发生了什么变化
使用输入和预期输出进行编辑
输入是一个int值数组,我将其转换为value,position的结构。例如输入是[25,4,3,-3,5,5,7,88,4,1],创建的结构是:
25 : 0
4 : 1
3 : 2
-3 : 3
5 : 4
5 : 5
7 : 6
88 : 7
4 : 8
1 : 9
我的代码按结构的value字段对值进行排序,然后删除任何重复的键值对。因此,预期结果将是:
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7
删除重复对(4,8)和(5,5)的位置。这是您的预期输出吗
Struct before sorting:
25 : 0
4 : 1
3 : 2
-3 : 3
5 : 4
5 : 5
7 : 6
88 : 7
4 : 8
1 : 9
Struct after sorting
-3 : 3
1 : 9
3 : 2
4 : 1
4 : 8
5 : 4
5 : 5
7 : 6
25 : 0
88 : 7
Sorting in the function:
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7
Struct after removing in the main
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7
只需在remove_duplicate()中将if条件从if(ptr[j].value!=ptr[i].value)更改为if(new_struct[j].value!=ptr[i].value)。你所说的“remove”到底是什么意思?你能提供一个输入和预期输出的例子吗?用输入和预期输出编辑文章。问题在于:
if(ptr[j].value!=ptr[i].value)
。只需使用一个较小的设置(例如4 5 5),您将很快看到发生了什么。修复很简单,所以我将把这一点留给您。您可能希望阅读以下内容:如果您想知道为什么程序的最终输出总是计数8行,这是由sizeof(*p2)
引起的。这正是我想要的。我试图指出,它不仅在remove_duplicate()函数中没有正确删除,而且在main函数中工作不正常,并且工作方式不同(没有打印最后一个结构)。这两种功能的工作原理应该相同。我需要将该指针正确地返回到main,以便稍后将其传递到其他函数。请问,这如何回答问题?只需在remove_duplicate()中将if条件从if(ptr[j].value!=ptr[I].value)更改为if(new_struct[j].value!=ptr[I].value)。没关系。我不知道我在说什么。成功了。
Struct before sorting:
25 : 0
4 : 1
3 : 2
-3 : 3
5 : 4
5 : 5
7 : 6
88 : 7
4 : 8
1 : 9
Struct after sorting
-3 : 3
1 : 9
3 : 2
4 : 1
4 : 8
5 : 4
5 : 5
7 : 6
25 : 0
88 : 7
Sorting in the function:
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7
Struct after removing in the main
-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7