簡體   English   中英

為什么下面的轉輪指針不會變為null?

[英]why does the runner pointer below not change to null?

為什么轉輪指針沒有變為null?

Node* runner = head->next;
Node* reversedList = head;
reversedList->next = nullptr;

但在下面,它確實變為null

Node* reversedList = head;
reversedList->next = nullptr;
Node* runner = head->next;

發出以下語句后

Node* runner = head->next;

'runner'指向'next'指向的內存(假設它是地址0x6543)。

(頭 - >下)------>內容<----(跑步者)

以下兩行:

Node* reversedList = head;

reversedList->next = nullptr;

因此,'next'現在指向NULL,但'runner'仍然指向先前由'next'指向的地址,即0x6543。

(head-> next) - > NULL | 內容<--------(跑步者)

第二個例子有效,因為首先你使head-> next指向NULL然后你讓'runner'指向head-> next,現在是NULL。 由於'head'和'reversedList'都指向相同的地址,第二個例子 - 沒有reverseList - 將是:

head->next = nullptr; 

Node* runner = head->next;

在第一個中,你將指針headhead->next分別存儲在reversedListrunner 然后你將reversedList->next更改為nullptr ,但是runnerhead-next的副本,而不是對指針本身的引用。 因此它沒有改變。

但是在第二個你做了reversedList->next nullptr然后制作了下一個指針的副本(你剛剛做了nullptr )因此副本是一個nullptr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM