contributed by <Louis-Wup
>
typedef struct list_item {
int value;
struct list_item *next;
} list_item_t;
typedef struct {
struct list_item *head;
} list_t;
void list_insert_before(list_t *l,
list_item_t *before,
list_item_t *item)
{
list_item_t **p;
for (p = &l->head; *p != before; p = &(*p)->next)
;
*p = item;
item->next = before;
}
首先,讓間接指標 p
指向 head
指標。
Learn More →
之後不斷判斷 p
指標指向的指標 是否指向 before
,也就是要插入的位置,若不是就將 p
指標指向 被指到的指標 所指向的節點的 next
指標。
Learn More →
Learn More →
最後讓 p
指標指到的指標 改成 指向 item
後,並讓 item
的 next
指向 before
。
Learn More →
Learn More →
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up