简体   繁体   中英

Function to compare structures in order to sort a list

I have an abstract data type in C, LIST OF THINGS, ist node has a void* pointer, what i'm trying to do is create a function to compare an specific field of different structures in order to sort my list of things.

typedef struct node{
     char *name;
     void *thing;
     struct node *next;   
}Node; 

This is the node i'm working with, i've already created a list of integers, list of structures and te compare function for both, but i can't figure out how to do a compare function to diferent structures. for example:

given these types:

 typedef struct main{
   float weight; 
   char*model; 
   float maxspeed; 
}Main;


typedef struct airplane{
   float weight; 
   float maxspeed; 
  }Airplane; 

typedef struct car{
   char*model;  
   float maxspeed;  
}Car;

And this is the function, so you have an idea of what i'm trying to do, it doesn't work, Main has fields that doesn't exist in either one or the other structure.

int comparefunction(void*a,void*b){
   Main a1, a2; 
   a1=*(Main*)a;  
   a2=*(Main*)b; 

   return a1.weight-a2.weight; 
}

This function(doesn't work) is passed as a paremeter to the function that links the nodes in order to use the comparefunction.

//insert prototype: 
 //insert(Node*listp,Node*newp,int(*func_comp)(void*,void*));  

list=insert(list,newItem(&car1),comparefunction); 
list=insert(list,newItem(&airplane1),comparefunction); 
list=insert(list,newItem(&airplane2),comparefunction); 

How can i do to compare a single field of two or more different structures? assuming that i know what each structure contains

Well, your car struct doesn't have a weight field, so I'm not exactly sure what you're trying to accomplish here. If you had your car struct look like

typedef struct car {
    float weight;
    char* model;
    float maxspeed;
} Car;

I think your function would work. Note, it is important that the member that you want to compare is at the same offset into each struct including the Main struct .

EDIT

This does work .

Another edit based on comments

You can't compare two completely unrelated things. This is not a technical deficiency with C. It just does not logically make any sense to compare for example an Airplane and an int .

If you're trying to compare somewhat similar things, you can look into unions.

struct attributes{
    float weight;
    // other common things?
};

struct thing {
    enum { Car, Main, Airplane } type;
    struct attributes attrs;
    union {
        struct Car car;
        struct Main main;
        struct Airplane airplane;
    } other_thing;
};

You'd change your list to store the thing struct, which is a structure that encapsulates all your possible types. The common elements of each type are extracted to the attributes struct. Your compare function would then operate on the attributes struct of the thing struct. The union is used here to only create enough space within struct thing for the largest of the union elements, so that you're not wasting space storing all three structs and only using one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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