contributed by <RusselCK
>
RusselCK
已知不存在 circular (環狀結構)
append_entry
: 新增節點,當 linked list 沒有內容時,必須由開發者更新指向開頭的指標。因此實際得到 reference ,而非 copyhead
的指標indirect
new_node
*indirect
取值是否為NULL,indirect
設為指向 (*indirect)->next
的指標(最後一個node)->next
的指標)append_entry
的功能定義來推論, #10#11
完成後還缺少將 new_node 放置在linked list 的步驟。因此先考慮 *indirect = new_node
的位置:
*indirect = new_node
放置在 AA1,則無法達成 append_entry
的功能。*indirect = new_node
放置在 AA2,可將 new_node 完美接在 linked list 之後。*indirect = new_node
assert(new_node)
The C library macro void assert(int expression) allows diagnostic information to be written to the standard error file. In other words, it can be used to add diagnostics in your C program.
#include <assert.h>
assert(int expression)
Source:https://www.tutorialspoint.com/c_standard_library/c_macro_assert.htm
swap_pair
: 交換一對相鄰的節點,取自 LeetCode: Swap Nodes in Pairs,給定 1->2->3->4,則該回傳 2->1->4->3for
迴圈
node_t **node = &head
:建立一個指向 head
的指標*node && (*node)->next
:確認 *node
及 (*node)->next
是否為 NULL ,皆不為 NULL 才可進入迴圈BB1
swap_pair
功能定義且已知BB1為迴圈的更新狀態,可推得BB1的效果為將 node
所指向的位址由 *node
的位址改為 (*node)->next->next
的位址node = &(*node)->next->next
*node=(*node)->next
reverse
: 將給定的 linked list 其內節點予以反向,即 1->2->3->4,則該回傳 4->3->2->1cursor
while
回圈: head
不為 NULL 時可進入迴圈 head->next = cursor; cursor = head
remove_entry
: 移除指定節點,指向開頭的指標可能因此變更,所以需要用到 a pointer to a pointer (指標的指標)swap_pair
和 reverse
,並避免回傳指標swap_pair
reverse
reverse
注意,你可能因此需要建立新的函式,如 rev_recursive
,隨後在 reverse
函式中呼叫 rev_recursive
運作原理(簡單示意):
rev_recursive(*head,cursor,head);
rev_recursive(next, head, list);
rev_recursive(next1, next, list);
The basic method given for generating a random permutation of the numbers 1 through N goes as follows:
Example (pancil-and-paper method):
Range | Roll | Scratch | Result |
---|---|---|---|
1 2 3 4 5 6 7 8 | |||
1-8 | 3 | 1 2 |
3 |
1-7 | 4 | 1 2 |
3 5 |
1-6 | 5 | 1 2 |
3 5 7 |
1-5 | 3 | 1 2 |
3 5 7 4 |
1-4 | 4 | 1 2 |
3 5 7 4 8 |
1-3 | 1 | 3 5 7 4 8 1 | |
1-2 | 2 | 3 5 7 4 8 1 6 | |
3 5 7 4 8 1 6 2 |
range
range
次的動作,依次取得結果,每完成一次,range
就減 1cursor
用來存放洗牌後的結果 ( Result )range
隨機選出編號 roll
roll
的 nodevalue
加入 cursor
head
刪除cursor
即為結果