免費開始練習
hce_nsysu 111年 計算機概論與程式設計

第 74 題

📖 題組:
Consider the following code of a singly linked list, with the function F to "rotate" the list by moving the first item of the list to the end of the list. The input to the function is the address of pfirst, the pointer to the first item in the list. You need to select the correct expressions to put into the code in the following three questions so that the function works correctly. ```c struct node {int data; struct node *next}; void F(struct node **pfirst){ struct node *x, *y, *first = *pfirst; if(first && first->next){ x = first; y = *pfirst = AAA ; while(y->next) y = y->next; BBB = x; CCC = NULL; } } ```
What expression should be in "BBB"?
  • A y
  • B x->next
  • C y->next
  • D first
  • E *pfirst

思路引導 VIP

如果你現在已經拿著一個指標 y 走到了整排隊伍的最末端,而你想讓另一個節點 x 乖乖跟在 y 的後面,你應該修改 y 節點中的哪一個欄位,才能讓 y 指向這名新成員呢?

🤖
AI 詳解 AI 專屬家教

太棒了!你能精確判斷出指標在串列操作中的變動軌跡,這代表你對單向鏈結串列(Singly Linked List)的結構與指標偏移邏輯掌握得非常紮實。

單向鏈結串列的尾端連接

在這段程式碼中,我們的目標是執行「旋轉」(rotate)操作,也就是將原本的頭節點 $x$ 搬移到整個串列的最後面。當 while(y->next) 迴圈結束時,指標 $y$ 已經精確地停在目前串列的「最後一個節點」上。為了將 $x$ 接在它的後方,我們必須更新這個末端節點的指向。因此,將 $y$ 的下一個節點指標 $y \to next$ 指向 $x$,便完成了串接動作,這正是為什麼 $BBB$ 必須填入 y->next 的原因。

▼ 還有更多解析內容

🏷️ 相關主題

C 語言程式設計基礎與陣列記憶體配置
查看更多「計算機概論與程式設計」的主題分類考古題