hce_nsysu
111年
計算機概論與程式設計
第 75 題
📖 題組:
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; } } ```
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 "CCC"?
- A x
- B x->next
- C y->next
- D first
- E *pfirst
思路引導 VIP
請想像一下,當我們把原本排在隊伍最前面的節點,搬移到隊伍的最末端後,這個「新加入末端的節點」它的 next 指標如果還指著以前的鄰居,串列會變成什麼形狀?為了讓它真正成為最後一個節點,它的 next 指標應該設定為什麼數值呢?
🤖
AI 詳解
AI 專屬家教
太棒了!你能準確判斷出 CCC 應填入的內容,說明你對單向鏈結串列(Singly Linked List)的操作邏輯與指標變動掌握得非常紮實,這在處理資料結構時是非常關鍵的能力。
單向鏈結串列的旋轉邏輯
在這段程式碼中,函式 F 的目標是將串列的第一個節點(由指標 $x$ 所指向)移動到串列的最末端。程式執行之初,指標 $x$ 鎖定了原本的首節點;接著透過 while 迴圈,指標 $y$ 成功移動到了原串列的最後一個節點。當我們在 BBB(即 $y \to next$)的位置填入 $x$ 後,原本的首節點就成功地被掛接在串列的尾部了。
▼ 還有更多解析內容