【问题标题】:Sorting dynamic array in a structure with C qsort ()使用 C qsort() 对结构中的动态数组进行排序
【发布时间】:2014-12-31 09:13:07
【问题描述】:

我在对结构中的数组(动态分配)进行排序时遇到问题。首先,想法是按升序对结构中的数组 i 进行排序。然后我想订购数组 i 维护而不是数组 j 与构造初始结构时获得的相同“关系”。我尝试为第一个想法工作,但 qsort 没有任何结果。所以这是我的代码......有什么想法吗?我觉得比较函数的构造有问题..

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int M =10;
int N =30;
int K = 10;

struct element {
int *i;
int *j;
int k;  
};

struct element *create_structure();
void print_element(struct element *);
int compare (const void *, const void * );
struct element * sort(struct element *);


main()
{
     srand(time(NULL));
     struct element *lista;
     int count;
     lista=create_structure();
     print_element(lista);
     printf("\n");
     lista=sort(lista);
}


 struct element *create_structure()
 {
         int aux1,aux2,count,load;
         struct element *structure;
         structure = (struct element *) malloc (M*sizeof(struct element *));
         structure->k=K;
         structure->i= (int *)malloc(structure->k*sizeof(int));
         structure->j=(int *)malloc (structure->k*sizeof(int));
            for (count = 0; count < K; count ++)
            {
               aux1=rand()%N;
               (structure->i)[count]=aux1;
                  do
                  {
                  aux2=rand()%N; 
                  }while(aux2==aux1);
               (structure->j)[count]=aux2;
            }
  return (structure);   
   }

   void print_element(struct element *lista)
   {
      int count;
      for(count = 0; count < K; count ++)
      {
         printf("%d     %d\n",lista->i[count],lista->j[count]);
      }
   }



   int compare(const void *a, const void *b)
     {
        struct element *ia = (struct element *)a; 
         struct element *ib = (struct element *)b; 
         int *ptr1=(ia->i);
         int *ptr2=(ib->i);
     return (*ptr1-*ptr2); 
     }


    struct element * sort(struct element *list)
     {
       qsort(list, sizeof(list->i)/ sizeof(int) , sizeof(list->i), compare);
      //qsort(list->i, K, sizeof(list->i), compare);
      print_element(list); 
      return (list);
     }

【问题讨论】:

  • 如何用qsort()对结构体的数组i进行排序?
  • 每次调用 create_structure 都会为你的结构分配一些内存并为成员填充值,所以如果你想这样做,那么你应该有链表来保存所有值然后对它们进行排序跨度>

标签: c arrays sorting dynamic qsort


【解决方案1】:

很抱歉迟到了! :)

所以让我们首先在你的代码中提到错误的语句

>>首先

在函数create_structure() 中你想为你的结构指针分配内存

struct element *structure; // here your structure pointer is 
//pointing to memory space of type struct element

structure = (struct element *) malloc (M*sizeof(struct element *));
                                       |------------------------|
                                                    |
                                                    V
Here you are allocating memory space of type struct element* which is 
wrong ! instead it must be sizeof(struct element)

关于同一个函数中的while循环,我发现它完全没用

aux1=rand()%N;
(structure->i)[count]=aux1; // the value is in aux1 variable 
do
 {
      aux2=rand()%N; 
 }while(aux2==aux1); // the loop try to get the same value of aux1 
                     // or you have just stored it in aux1
(structure->j)[count]=aux2; // it is easy to delete the while loop and
                            // change aux2 by aux1

>>第二

关于排序

qsort(list, sizeof(list->i)/ sizeof(int) , sizeof(list->i), compare);
     |-----|
        |
        V
It is not an adress of the array so it is Wrong !

在了解了这里的主要问题后,根据自己的代码编写了一个完美运行的代码版本

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int M =10;
int N =30;
int K = 10;

struct element {
    int *i;
    int *j;
    int k;
};

struct element *create_structure();
void print_element(struct element *);
int compare (const void *, const void * );
void sort(struct element *); // changed the return value of sort
// to void as the argument will be changed directly because it is a
// pointer 


int main()
{
    srand(time(NULL));
    struct element *lista;
    lista=create_structure();
    printf("\n--------- i ---  j  ---------\n\n");
    print_element(lista);
    printf("\n---------------------------\n");

    sort(lista);
    print_element(lista);
    return 0;

}


struct element *create_structure()
{
    int aux1=0,count=0;
    struct element *structure;
    // Changed the allocation of structure pointer 
    structure = (struct element *) malloc (sizeof(struct element));
    structure->k=K;
    structure->i= (int *)malloc(K*sizeof(int));
    structure->j=(int *)malloc (K*sizeof(int));
    for (count = 0; count < K; count ++)
    {
        aux1=rand()%N; 
        // we kept only the first aux1 and copied it in the two arrays
        (structure->i)[count]=aux1;
        (structure->j)[count]=aux1;
    }
    return (structure);
}

void print_element(struct element *lista)
{
    int count=0;
    for(count = 0; count < K; count++)
    {
        printf("row=%2d :  %2d     %2d\n",count+1,(lista->i)[count],(lista->j)[count]);
    }
}


int compare(const void *a, const void *b)
{
    // compare the values of two case of array pointed by i of type int
    return *(int*)a-*(int*)b;
}


void sort(struct element *list)
{
  // we will sort the array pointed by i which contains K elements 
  // of type int and size sizeof(int) by using the compare function
    qsort(list->i, K , sizeof(int), compare);
}

希望对您有所帮助! :)

注意:在代码块 v13.12 中使用此代码在 linux(gcc 版本 4.8.2)下会生成错误输出!! [可能是 Code::Blocks 中的 BUG] 但在 gcc 的命令行中使用它会给出正确的输出!!

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多