gpt4 book ai didi

linux - 如何使用 功能对列表进行排序?

转载 作者:太空狗 更新时间:2023-10-29 11:20:20 24 4
gpt4 key购买 nike

我使用包含

sys/queue.h 功能创建了一个列表
struct stInside{
int a;
int b;
};

struct stOutside{
struct stInside in;
LIST_ENTRY(stOutside) outq;
};

LIST_HEAD(stOutsideHead, stOutside) head = LIST_HEAD_INITIALIZER(head);
struct stOutsideHead *headPtr;

struct stOutside *list;

for(int i=0; i < 4; i++){
list = malloc(sizeof(struct stOutside));
list->in.a = i;
list->in.a = i;
LIST_INSERT_HEAD(&head, list, outq);
}

我想知道如何使用什么,以便根据结构 stInside 的 a 字段对该列表进行排序。是否有任何特定的 MACROS 可以完成这项工作?我看到一个

#define LIST_SORT_PROTOTYPE(name, type, field, cmp)             \
QHELPER_SORT_PROTOTYPE(LIST, name, type, field, cmp)

sys/queue.h 中,但我不明白它是如何工作的。

非常感谢您的分享和您的宝贵时间。

最佳答案

this example .它使用 SLIST 而不是 LIST,但思想是一样的。

基本上,无论您在何处使用排序函数原型(prototype),您都使用LIST_SORT_PROTOTYPE(...),在任何您将使用排序函数定义的地方使用LIST_SORT_GENERATE(...),和 LIST_SORT(...) 无论您在哪里调用该函数。

关于linux - 如何使用 <sys/queue.h> 功能对列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com