【问题标题】:How to sort list using <sys/queue.h> functionality?如何使用 <sys/queue.h> 功能对列表进行排序?
【发布时间】:2011-10-22 20:05:33
【问题描述】:

我使用 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);
}

我想知道 How & What 用于根据 struct stInside 的 a 字段对该列表进行排序。是否有任何特定的宏可以完成这项工作?我看到了一个

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

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

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

【问题讨论】:

    标签: linux macros queue linked-list


    【解决方案1】:

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

    基本上,您在需要使用排序函数原型的地方使用LIST_SORT_PROTOTYPE(...),在需要使用排序函数定义的地方使用LIST_SORT_GENERATE(...),在需要调用该函数的地方使用LIST_SORT(...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2014-05-30
      • 2018-07-07
      • 2011-07-09
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多