gpt4 book ai didi

c - 从数组中删除结构

转载 作者:行者123 更新时间:2023-11-30 18:11:05 25 4
gpt4 key购买 nike

我正在尝试实现一个循环遍历已排序的结构数组的函数,如果“键”(第一个字段值)有重复项,它将保留该键值对的第一次迭代,并删除之后出现的任何重复项。这是我的代码:

#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;
}

在remove_duplicates()函数中,我期望这是我的结果:

-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
7 : 6
25 : 0
88 : 7

但是,我得到了一个额外的键值 5:5,但它没有被删除:

-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
5 : 5
7 : 6
25 : 0
88 : 7

我试图手动循环它,我认为我在 j++ 上犯了一个错误,因为似乎当条件为假时(即有重复),i 迭代而 j 留在后面,我认为这可能就是为什么我删除了重复的 4 但没有删除重复的 5。

我哪里出错了?我对对索引 0 处的值进行硬编码也感到很奇怪,但显然第一个值不是我想要删除的值(只有重复的后续值需要删除),并且由于我从 1 开始,所以它不会没有进行比较。

最后,remove_duplicate() 函数返回 struct * 类型。当我从主打印时,我得到以下输出:

-3 : 3
1 : 9
3 : 2
4 : 1
5 : 4
5 : 5
7 : 6
25 : 0

由于某种原因没有指向最后一个值。我希望我的删除函数可以从主函数访问,因为我想将指针 p2 传递给另一个按键值对中的值排序的函数。我的最后一个值发生了什么?

使用输入和预期输出进行编辑

输入是一个 int 值数组,我将其转换为一个值、位置结构。例如输入是 [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

我的代码按结构的值字段对值进行排序,然后删除任何重复的键值对。所以预期的结果是:

-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) 即可。

关于c - 从数组中删除结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52313873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com