我的大學教授給了我們以下代碼,但我覺得倒數第三行應該是
temp -> next = curr -> next -> next
如果我弄錯了,為什么它是正確的?
!!注意,新節點應該放在鏈表的中間,而不是邊緣!
void insert_list (struct node *head, int pos) {
int k;
struct node *temp, *curr;
curr = ihead;
for (k=1; k<pos; k )
curr = curr -> next;
temp = (struct node *) malloc (sizeof (struct node));
temp -> next = NULL;
gets (temp -> student);
scanf ("%d", &temp -> am);
temp -> next = curr -> next;
curr -> next = temp;
}
uj5u.com熱心網友回復:
如果您有元素A B C D并且在 B 和 C 之間插入 X,則:
在一開始的時候:
- B 將是
curr - C將是
curr->next - D將是
curr->next->next
在您的功能之后:
- B 仍然是
curr - X 將是
curr->next和temp - C 將是
temp->next和curr->next->next - D 將是
temp->next->next和curr->next->next->next
以便有鏈表:A B X C D.
如果您使用curr->next->next,X->next將指向 D 并且您將在此程序中丟失 C 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485102.html
上一篇:c中的stride是什么
