我遇到過兩種方式來實現雙向鏈表:這兩種不同的實現雙鏈表的方式有什麼區別?
#define LIST_ENTRY(type) \
struct { \
struct type *le_next; /* next element */ \
struct type **le_prev; /* address of previous next element */ \
}
這種方式是在FreeBSD的queue.h
,我想知道爲什麼它採用指針的指針le_prev
?
struct list_head {
struct list_head *next, *prev;
};
這種方式是在linux list.h
,它只是使用兩個簡單的指針。
有什麼區別?哪個更好,只需使用一個簡單的指針和一個指向指針的指針或兩個簡單的指針?
它依賴於語義。單指針將指向前一個或下一個節點或元素另一方面可以使用雙指針,其中指針將再次指向包含其他節點的鏈接列表。 –
你確定雙指針是用於「上一個下一個元素」嗎? –
http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/queue.h?rev=1.30 –