Board logo

標題: APCS_觀念題_10503_6 (練習) [打印本頁]

作者: 方浩葦    時間: 6 天前 10:39     標題: APCS_觀念題_10503_6 (練習)

本帖最後由 方浩葦 於 2024-9-14 10:43 編輯

List 是一個陣列,裡面的元素是 Node,每個 Node 具有 value 和 next 兩個屬性。value 是一個整數,next 是指向陣列中下一個 Node 的索引,如果沒有下一個節點,next 設為 -1。

現在定義一個函數 RemoveElementAtIndex,該函數會移除串列中某個指定的 index 元素,並保持串列的連接。假設函數在呼叫時,會提供一個有效的 head 和 index,head 是串列的起始節點索引,index 是要移除的節點索引。假設我們只能一次遍歷串列,並且不能直接修改 head 節點。請問,該函數應如何實現?
  1. struct Node{
  2.     int value;
  3.     int next;
  4. };

  5. void RemoveElementAtIndex(Node list[], int head, int index) {
  6.     int current = head;

  7.     if (head == index) {
  8.         printf("無法移除起始節點\n");
  9.         return;
  10.     }

  11.     while (list[current].next != -1) {
  12.         if (list[current].next == index) {
  13.             __________________________________________
  14.             break;
  15.         }
  16.         current = list[current].next;
  17.     }
  18. }
複製代碼
請問在空白處應該填入的程式碼為何?

(A) list[current].next = current;
(B) list[current].next = list[index].next;
(C) list[index].next = list[current].next;
(D) list[index].next = -1;

本帖隱藏的內容需要積分高於 1 才可瀏覽





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2