Description
Please complete the following three functions:
請完成以下三個函式:
InsertNode(Node* node, int find, int item): Create a new node with the value item and insert it after the node with the value find. There won't be a situation where find cannot be found. This function does not need to return anything.
InsertNode(Node* node, int find, int item):創建一個新的節點(值為 item ),並插入到值為 find 的節點後方,不會有找不到 find 的情況。此函式不須進行回傳。
DeleteNodebyValue(Node* head, int num): Delete the node with the value num and return the head of the Linked list. If there are two nodes with the same value num, only delete the one that appears first. However, if num cannot be found, no deletion is needed. Print "Failed to delete." and return the head pointer directly.
DeleteNodebyValue(Node* head, int num):刪除值為 num 的節點,並回傳Linked list的開頭。如果同時有兩個節點的值為 num,只需要刪除比較前面的那一個。但如果找不到num,則不需做刪除,印出 "Failed to delete."後直接回傳開頭指標即可。
PrintList(Node* head): Print the values of all nodes in their original order. If it's empty, print "Empty". This function does not need to return anything.
PrintList(Node* head):照原本順序印出所有節點的值。如果是空的則印出"Empty",此函式不需進行回傳。
Input
The above functions "do not" require additional user input; everything needed will be directly provided as input!
上述函式都"不需要"額外自己輸入值,要用到的都會直接作為傳入值!
The first line contains an integer n, representing the number of nodes in the original linked list. Then the second line contains n numbers, representing the values in the linked list.
第一行輸入包含一個整數 n,代表原始 linked list 內的 node 個數。接下來第二行有 n 個數字,代表linked list內的值。
Starting from the third line, there are multiple lines of input. Each line starts with a number choice representing the chosen instruction:
第三行開始有多行輸入,每行第一個數字 choice 代表選擇的指令:
choice=0: Input ends
choice=0: 輸入結束
choice=1: InsertNode, followed by two numbers find and item
choice=1: InsertNode,後面輸入的兩個數字分別為find, item
choice=2: DeleteNodebyValue, followed by one number num to be deleted
choice=2: DeleteNodebyValue,後面輸入的一個數字為要刪除的值 num
choice=3: PrintList
choice=3: PrintList
Output
Please see the description above.
請直接參考 Description。
Sample Input 1
3
7 4 2
1 7 2
1 2 5
3
2 7
2 5
3
1 4 1
3
2 3
2 1
3
2 2
2 4
3
2 2
3
3
0
Sample Output 1
7->2->5->4->2
2->4->2
2->4->1->2
Failed to delete.
2->4->2
2
Empty
Empty
Hint