contributed by <As7r1d
>
主要有兩個測試:
for (size_t i = 0; i < N; i++)
list_insert_before(&l, NULL, &items[i]);
for (size_t i = 0; i < N; i++)
list_insert_before(&l, NULL, &items[i]);
串列架構如下:
#include <stddef.h>
typedef struct list_item {
int value;
struct list_item *next;
} list_item_t;
typedef struct {
struct list_item *head;
} list_t;
list_item_t
表示串列的節點。其中 value
表示節點儲存的數值
,next
則表示指向下一個 list_item_t
節點的指標(若為 NULL,表示是最後一個節點)。
list_t
中 head 表示指向串列的第一個節點。如果 head 是 NULL,表示鏈結串列為空。
list_insert_before
在這個函式中目的是將 item 插入 before 節點之前。
void list_insert_before(list_t *list, list_item_t *before, list_item_t *item) {
list_item_t **p;
for (p = &list->head; *p != before; p = &(*p)->next)
;
*p = item;
item->next = *p;
}
list_item_t **p
,p 是個變數指向 list_item_t *
指標的指標。p = &list->head
表示一開始 p 指向 head 節點指標的指標。*p != before
檢查目前 p 指向的節點是否是目標節點(before)。p = &(*p)->next
如果不是,就移動到下一個節點的指標。如果找到,*p = item
指向 before 的指標改指向 item。item->next = before
將 item 的 next 指向 before,完成插入。
typedef struct block {
size_t size;
struct block *l, *r;
} block_t;
void remove_free_tree
函式
block_t **find_free_tree(block_t **root, block_t *target);
block_t *find_predecessor_free_tree(block_t **root, block_t *node);
void remove_free_tree