简体   繁体   English

用于比较结构以便对列表进行排序的功能

[英]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. 我在C中有一个抽象的数据类型,事物的清单,ist节点有一个void *指针,我试图做的是创建一个函数来比较不同结构的特定字段,以便对事物列表进行排序。

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. 这是我正在使用的节点,我已经创建了一个整数列表,结构列表和te比较函数,但是我不知道如何对不同的结构执行比较函数。 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. 这就是函数,因此您对我要执行的操作有一个了解,它不起作用,Main具有在一个或另一个结构中都不存在的字段。

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. 此功能(不起作用)作为参数传递给链接节点的功能,以便使用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. 好吧,您的car结构没有 weight域,所以我不确定您要在这里完成什么。 If you had your car struct look like 如果您的car结构看起来像

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 . 请注意,要比较的成员到每个结构( 包括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 . 这不是C的技术缺陷。例如,将Airplaneint进行比较在逻辑上没有任何意义。

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. 每种类型的公共元素都提取到属性struct中。 Your compare function would then operate on the attributes struct of the thing struct. 然后,您的compare函数将对事物结构的属性结构进行操作。 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. 这里使用并集仅在结构事物中为最大的并集元素创建足够的空间,因此您不会浪费存储所有三个结构而仅使用一个的空间。

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

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