2014-11-04 133 views
0

我在排序結構中的數組(動態分配)時遇到問題。首先,這個想法是按照順序排列結構中的數組i。然後我想要命令我維護的數組,而不是數組j在構造初始結構時獲得相同的「關係」。我嘗試爲第一個想法工作,但沒有任何qsort的結果。所以這是我的代碼...任何想法?我覺得在比較功能的構建中存在問題..使用C 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); 
    } 
+0

如何使用qsort()命令結構的數組i? – Andres 2014-11-04 13:08:32

+0

每次調用create_structure都會爲結構分配一些內存併爲成員填充值,所以如果你想這樣做,那麼你應該有鏈表來保存所有的值然後對它們進行排序 – Gopi 2014-11-04 13:24:47

回答

0

對不起,遲到了! :)

因此,讓我們第一次開始在代碼中提到的錯誤陳述

>>首先

在功能 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給出正確的輸出!