課堂問答簡記
Q: 將 add_entry
裡的 new_node
插在 head
的前面要怎麼改?
如果傳進來的是前面 node 的 next 的 reference,那直接 dereference 後接上去就可以了
e.g. add_entry(&(head->next), 109);
但如果不是的話,e.g. node_t *head2 = head->next; add_entry(&head2, 109);
,就變成用下面的方法,加一個 node 在後面,然後把資料移過去
Q 如何改 reverse 為 pointer to pointer
Q 將 add_entry
裡的 new_node
插在 list 的最前面要怎麼改? (head前面可能還有node,請在確保整體linked list依舊正常的情況下在head前面插入new_node
.另外,不可新增傳入的參數)
我贊同 ccs100203
的方法,以下內容皆參考 ccs100203
的答案
head'
是參數的(*head)
我們並不知道紫色的那些東西存不存在,所以新增一個 node ,把 node2 的 value 丟給 new node
copy value of node2 to new node
copy the new value to node2
這樣就可以在維持 link list 的正確性的情況下,插入一個node