繁体   English   中英

使用结构数组在C中实现排序算法时遇到问题

[英]Problem implementing sorting algorithm in C with an array of structs

好吧,这是我的小问题,首先是我的代码:


struct alumn {
    char name[100];
    char lastname[100];
    int par;
    int nota;
};

typedef struct alumn alumn;

int bubble(alumn **arr, int length) { int i,j; alumn *temp;

for (i=0; i<=length-2; i++) {
    for (j=i+1; j<=length-1;j++) {
        if ((*arr)[i].nota > (*arr)[j].nota) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}

}

int main(int argc, char **argv) { alumn *alumns;

... here goes some other code ...

bubble(&alumns,totalAlumns);

return 0;

}

我的问题是该算法未对任何内容进行排序。 我在进行交换时遇到了困难,我尝试了一切,但没有任何效果:(。任何帮助???

显然,您正在将铝结构 的数组与指向铝结构的指针的数组混淆

Bubble逻辑扩展了指针数组,由此主函数似乎使用结构数组对其进行调用。

由于校友结构的大小,对指针执行冒泡排序可能会更有效,因为每次交换都需要更少的数据移动(一个指针的3个副本每个字节几个字节,而校友的3个副本struct,每个200+字节!)。

我建议您修改main()函数的逻辑(问题片段中未显示),以将这样的指针数组引入实际的校友结构。 (当然,该指针数组不会使您自己也分配结构体,en块(在结构体数组中)或单独分配结构体。


在您的坚持下,我在这里提示main如何看起来像产生一个可用于bubble的指针数组(bubble保持不变)。
顺便说一句,我将alumn *alumns[]声明为alumn *alumns[] ,它更容易显示其意图。 这和alumn **alumns

int main(int argc, char **argv)
{
    alumn *alumns[];   // changed to array of pointers [to alumn structs] 
                       // was pointer to alumn struct, likely to be used as an array thereof

    int MaxNbOfAlumns = some_limit;
    alumns = malloc(sizeof(*alumn) * MaxNbOfAlumns);

    // Load the alumn records (from file or whereever)
    // pseudo code:
    // int i_alumns = 0;  // points to the next free slot in alumns array
    // for each record (in file or whereever...)
    //     alumms[i_alums] = malloc(sizeof(struct alumn));
    //     strcpy(alumms[i_alums]->lastname,  whatever_data);
    //     strcpy(alumms[i_alums]->name,  whatever_otherdata);
    //     alumms[i_alums]->par = some_int_data;
    //     alumms[i_alums]->nota = some_other_int_data;
    //     i_alums++;

... here goes some other code ...

  bubble(alumns, totalAlumns);   // alumns now being an array can be passed as is.

  return 0;
}

另外,如果您希望像以前一样保留原始的校友变量,那么在调用bubble()之前,可能需要做的就是这样

  int i;
  alumn *ap_alumns[];   // new variable
  ap_alumns = malloc(sizeof(*alumn) * totalAlumns);
  for (i = 0; i < totalAlumns; i++)
      ap_alums[i] = &alumns[i];
  bubble(ap_alumns, totalAlumns);  

应该强调的一件事是,不管其起源如何,传递给bubble()的数组都可以正确排序,但是要使用它,您需要取消引用各个指针。
因此与旧阵列您打算在说使用alumns[123].lastname ,你现在需要alumns[123]->lastname (或ap_alumns[123]->lastname ,如果你使用第二版)。

您的代码不起作用,因为您有一个结构数组而不是指针数组。

当您尝试交换两个结构时,=运算符不知道该怎么做。 您必须一一复制该结构的字段才能正常工作。

如果您有一个指针数组来代替明矾结构的实例。 这样代码就可以了,因为您正在分配指针。 指针基本上是一个数字,并且=知道如何复制数字。

您的代码被编写为好像有一个指针数组,但是遗漏了代码的重要部分(即... here goes some other code ... ),所以我们看不到您如何进行设置要分类的东西。 如果您有一个指针数组,则此行代码:

if ((*arr)[i].nota > (*arr)[j].nota) {

应该:

if (arr[i]->nota > arr[j]->nota) {

原因是,(* arr)获取列表中的第一个指针,然后(* arr)[i]获取该指针之后的内存中的第i个项目(无意义,因为每个指针都应指向一个项目)。 第二种语法转到指针数组中的第i个指针,并对其取消引用以获取该值。

也许此插图将有助于:

arr points to an array of two pointers, each pointing to a struct.
(*arr)[1].nota refers to an item in ??????.
arr[1]->nota refers to an item in struct 2.

       +---+                   +----------+
arr -> | * | ----------------> | struct 1 |
       +---+    +----------+   +----------+
       | * | -> | struct 2 |   :  ??????  :
       +---+    +----------+   +..........+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM