排序动态数组在用C的qsort的结构() [英] Sorting dynamic array in a structure with C qsort ()

查看:200
本文介绍了排序动态数组在用C的qsort的结构()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在排序的结构数组(动态分配)问题。首先,当时的想法是订购结构数组我在方兴未艾的顺序。然后,我就在想订购阵列I保持与而不是当它被建造的初始结构得到相同的关系的阵列学家我尝试的第一个想法,但又不想与任何qsort.So结果,这是我的code ...任何想法?我认为这是在比较函数的构造问题。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&math.h中GT;INT M = 10;
INT 30例;
INT K = 10;结构元素{
INT *我;
INT *焦耳;
时int k;
};结构元件* create_structure();
无效print_element(结构元素*);
INT比较(常量无效*,常量无效*);
结构元素*排序(结构元素*);
主要()
{
     函数srand(时间(NULL));
     结构元素* LISTA;
     诠释计数;
     LISTA = create_structure();
     print_element(LISTA);
     的printf(\\ n);
     LISTA =排序(LISTA);
}
 结构元素* create_structure()
 {
         INT AUX1,AUX2,计数,负荷;
         结构元素*结构;
         结构=(结构元素*)malloc的(M *的sizeof(结构元素*));
         结构 - > k = K;
         结构 - I标记=(INT *)malloc的(结构 - > K *的sizeof(INT));
         结构 - > J =(INT *)malloc的(结构 - > K *的sizeof(INT));
            为(计数= 0; COUNT< k;计数++)
            {
               辅助1 =兰特()%N;
               (构筑物I标记)[计] = AUX1;
                  做
                  {
                  AUX2 =兰特()%N;
                  }而(AUX2 == AUX1);
               (结构 - > j)条第[count] = AUX2;
            }
  返回(结构);
   }   无效print_element(结构元素* LISTA)
   {
      诠释计数;
      为(计数= 0; COUNT< k;计数++)
      {
         的printf(%D \\ n,l​​ista- I标记[计],lista->Ĵ[统计]);
      }
   }   INT比较(常量无效*一,常量无效* B)
     {
        结构元素* IA =(结构元素*)一个;
         结构元素* IB =(结构元素*)B:
         为int * ptr1的=(IA-I标记);
         为int * PTR2 =(IB-I标记);
     返回(* ptr1- * PTR2);
     }
    结构元素*排序(结构元素*名单)
     {
       的qsort(列表的sizeof(列表 - I标记)/的sizeof(INT)的sizeof(列表 - I标记),比较);
      //的qsort(列表 - I标记,K的sizeof(列表 - I标记),比较);
      print_element(名单);
      返回(列表);
     }


解决方案

对不起来晚了党! :)

因此​​,让我们第一次开始被提在code中的错误的语句

>>首页

在功能 create_structure()要为您结构指针分配内存

 结构元素*结构; //此处是您结构指针
//指向类型的结构元素的内存空间结构=(结构元素*)malloc的(M *的sizeof(结构元素*));
                                       | ------------------------ |
                                                    |
                                                    V
在这里,你是分配结构类型元素的内存空间*这是
错了!相反,它必须与sizeof(结构件)

关于我发现这是完全无用相同功能的while循环

  AUX1 =兰特()%N;
(构筑物I标记)[计] = AUX1; //值是在AUX1变量

 {
      AUX2 =兰特()%N;
 }而(AUX2 == AUX1); //环路试图获得辅助1的相同的值
                     //或者你刚刚保存它AUX1
(结构 - > j)条第[count] = AUX2; //很容易删除while循环和
                            //将AUX1 AUX2改变

>>二

关于排序

 的qsort(列表的sizeof(列表 -  I标记)/的sizeof(INT)的sizeof(列表 -  I标记),比较);
     | ----- |
        |
        V
它不是阵列的ADRESS所以它是错误的!

知道这里的​​主要问题后根据自己的code版本code的这完美的作品

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
#包括LT&;&math.h中GT;INT M = 10;
INT 30例;
INT K = 10;结构元素{
    INT *我;
    INT *焦耳;
    时int k;
};结构元件* create_structure();
无效print_element(结构元素*);
INT比较(常量无效*,常量无效*);
无效的排序(结构元素*); //改变排序的返回值
//作废作为参数将直接改变,因为它是一个
//指针
诠释的main()
{
    函数srand(时间(NULL));
    结构元素* LISTA;
    LISTA = create_structure();
    的printf(\\ n ---------我---Ĵ--------- \\ n \\ n);
    print_element(LISTA);
    的printf(\\ n --------------------------- \\ n);    排序(LISTA);
    print_element(LISTA);
    返回0;}
结构元素* create_structure()
{
    INT辅助1 = 0,计数= 0;
    结构元素*结构;
    //改变结构指针的分配
    结构=(结构元素*)malloc的(的sizeof(结构件));
    结构 - > k = K;
    结构 - I标记=(INT *)malloc的(K *的sizeof(INT));
    结构 - > J =(INT *)malloc的(K *的sizeof(INT));
    为(计数= 0; COUNT< k;计数++)
    {
        辅助1 =兰特()%N;
        //我们只保留了第一AUX1和两个阵列复制它
        (构筑物I标记)[计] = AUX1;
        (结构 - > j)条第[count] = AUX1;
    }
    返回(结构);
}无效print_element(结构元素* LISTA)
{
    诠释计数= 0;
    为(计数= 0; COUNT< k;计数++)
    {
        的printf(行=%2D:2D%2D%\\ n,计数+ 1,(lista- I标记)[统计](lista-> j)条[计数]);
    }
}
INT比较(常量无效*一,常量无效* B)
{
    //比较两个案例阵由我int类型的指出值
    返回*为(int *)A - *为(int *)B:
}
无效的排序(结构元素*名单)
{
  //我们将通过我所指向的数组,它包含k个元素进行排序
  通过使用比较函数int类型和尺寸的sizeof(INT)的//
    的qsort(列表 - I标记,K的sizeof(int)的,比较);
}

希望它帮助! :)

请注意:使用$ C $个cblocks这个code v13.12 linux下生成(gcc版本4.8.2)输出错误! [可能是在code一个BUG :: Blocks的]
但使用它的命令行用gcc给出了正确的输出!

I have a problem to sort an array (dynamically allocated) in a structure. Firstly, the idea was to order the array i in the structure in an ascendant order. Then I was thinking to order the array i maintaining instead the array j with the same "relationship" obtained when it was constructed the initial structure. I try to work for the first idea, but without any result with qsort.So this is my code... Any ideas? I think there is a problem in the construction of the comparing function..

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

解决方案

Sorry for being late to the party ! :)

So let's start first by mentioning the wrong statements in your code

>> First

in function create_structure() you want to allocate memory for your structure pointer

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)

Concerning the while loop in the same function I found that it is totally useless

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

>> Second

Concerning the sort

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

after knowing the major problems here is a version of code based on your own code which works perfectly

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

Hope it helps ! :)

Note: Using this code in codeblocks v13.12 generates under linux (gcc version 4.8.2) wrong output !! [ It might be a BUG in Code::Blocks] but using it with command line with gcc gives correct output !!

这篇关于排序动态数组在用C的qsort的结构()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆