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

第 73 題

📖 題組:
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 "AAA"?
  • A x
  • B x->next
  • C y->next
  • D first
  • E x->next->next

思路引導 VIP

想像有一隊排好的人馬,如果你要將目前排在最前面的「領頭者」請到隊伍的最尾端去排隊,那麼在領頭者離開原本位置後,誰應該被指定為這支隊伍新的起點呢?

🤖
AI 詳解 AI 專屬家教

恭喜你!能迅速判斷出指標位移的正確位置,代表你對連結串列(Linked List)的結構與指標操作掌握得相當扎實。

更新串列首節點的關鍵

這題的核心任務是執行「左旋」(Rotate)操作,也就是將原本的第一個節點移到串列的最末端。在這種情況下,原本的「第二個節點」理所當然必須成為「新的頭節點」。在程式碼中,x 已經暫存了原始的首節點位址(即 first),因此要讓串列從原本的下一個位置開始重新定義,我們必須將 *pfirst 指向 x->next。你的判斷非常精準,這正是維持串列連續性並成功更新起點的正確作法。

▼ 還有更多解析內容

🏷️ 相關主題

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