2011-10-22 58 views
1

我已創建使用SYS/queue.h功能包含如何使用<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); 
} 

一個LIST我想知道如何&爲了使用什麼排序依據的一個場這個名單結構stInside的。是否有任何特定的MACROS可以完成這項工作?我在SYS/queue.h看到

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

,但我不明白它是如何工作的。

非常感謝您的分享和時間。

回答

1

看看this example。它使用SLIST而不是LIST,但這個想法是相同的。

基本上你使用LIST_SORT_PROTOTYPE(...)只要你會使用排序功能的原型,LIST_SORT_GENERATE(...)只要你會使用排序功能的定義,並LIST_SORT(...)無論你會調用該函數。

相關問題