我想这对于 C 来说根本不可能。但我必须问一下才能确定。
我有两个具有相同成员的结构,我想对其列表进行排序。
struct a
{
tm seconds;
struct a *next;
/* further stuff */
};
struct b
{
tm seconds;
struct b *next;
/* further, but different stuff as in a */
};
我正在寻找一种仅具有单个函数来对这种类型的串联列表进行排序的可能性。
类似的东西
sort(void struct *locallist)
{
/* do sort for locallist->seconds */
return;
}
对于两种类型的结构来说它应该是通用的,因为只要我可以根据“*秒”进行排序,不同的内容根本不是什么问题。
有什么想法吗?
/克内布
我怀疑您正在寻找的实际解决方案是这样的:
typedef enum
{
I_AM_A,
I_AM_B,
} what_am_i;
struct node
{
tm seconds;
struct node *next;
what_am_i id;
void* data; // points at A or B specific parts
};
也就是说,使用 same 结构类型,但将特定部分存储在其他地方。
data
可能会转换为 struct a*
或 struct b*
,具体取决于 id
。