# 2020q3 Homework1 (quiz1) contributed by < `fdfdd12345628` > ## AA1, AA2 在前面的程式已經新增一個node,並且將裡面的初始值都訂好了,因此AA1是為了確認該node是否有效,所以為`assert(new_node)` 接著程式會跑到該list最後,並且加上新的node,因此AA2為`*indirect = new_node`將最後加上新的node ## BB1, BB2 由於要每兩個就交換一次,所以理論上每個迴圈都要把指標改成下下個節點,因此在 for 回圈內的 BB1 要換到下下個節點 又考慮到 node 是 pointer 的 pointer ,因此必須要先 dereference 後,才可以取 next 的 next ,最後 assign 時也要將其取址後,才符合 pointer 的 pointer ,因此 BB1 為`node = &(*node)->next->next` 在迴圈內,已經有 `tmp` 備份 `node` ,所以可以直接將操作自己的 next ,所以BB2為`*node = (*node)->next` `node_t *tmp = *node;` ```graphviz digraph G{ node [shape = record]; current [lable="*node"] tmp -> node0 current -> node0 node0 -> node1 node1 -> node2 } ``` `*node = (*node)->next` ```graphviz digraph G{ node [shape = record]; current [lable="*node"] tmp -> node0 current -> node1 node0 -> node1 node1 -> node2 } ``` `tmp->next = (*node)->next;` ```graphviz digraph G{ node [shape = record]; current [lable="*node"] tmp -> node0 current -> node1 node0 -> node2 node1 -> node2 } ``` `(*node)->next = tmp;` ```graphviz digraph G{ node [shape = record]; current [lable="*node"] tmp -> node0 current -> node1 node0 -> node2 node1 -> node0 } ``` ## CCC 要將linked list進行reverse,所以必須要儲存當下的node,以及前一個node,並且將當下node位址備份起來,把當下node的next指向前一個,並且將當下的node切換成下一個,前一個node也切換成當下,直到結束。 將下一個node位址備份起來 `node_t *next = head->next;` ```graphviz digraph G{ node [shape = record]; cursor -> "node-1" head -> node0 next -> node1 node0 -> node1 } ``` 將當下node指向前一個 `head->next = cursor;` ```graphviz digraph G{ node [shape = record]; next [lable="*node"] head -> node0 cursor -> "node-1" node0 -> "node-1" next -> node1 } ``` 將cursor指向當下,供下次迴圈使用 `cursor = head` ```graphviz digraph G{ node [shape = record]; next [lable="*node"] head -> node0 cursor -> node0 node0 -> "node-1" next -> node1 } ``` 將head指向當下,供下次迴圈使用 `head = next;` ```graphviz digraph G{ node [shape = record]; next [lable="*node"] head -> node1 cursor -> node0 node0 -> "node-1" next -> node1 } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up