簡體   English   中英

指針如何在鏈表中工作(Java)

[英]How do pointers work in a linked list (Java)

/**
 * Definition for polynomial singly-linked list.
 * class PolyNode {
 *     int coefficient, power;
 *     PolyNode next = null;
 
 *     PolyNode() {}
 *     PolyNode(int x, int y) { this.coefficient = x; this.power = y; }
 *     PolyNode(int x, int y, PolyNode next) { this.coefficient = x; this.power = y; this.next = next; }
 * }
 */
       PolyNode iter1 = poly1;
       PolyNode poly1 = null;
       while(iter1 != null){
           PolyNode next = iter1.next;
           iter1.next = poly1;
           poly1 = iter1;
           iter1 = next;
       }

我對上面的 while 循環很困惑。 我不知道這個 while 循環將如何處理鏈表 poly1。 請幫幫我!

在 while 循環中,第一行創建了“iter1.next”的副本。 第二行使“iter1”指向“poly1”。 第三行讓'poly1'變成'iter1'。 第 4 行讓 iter1 成為“下一個”。

請糾正我錯誤的地方,因為我試圖從上面的邏輯中繪制圖表。 這對我來說不太有意義。

它不復制任何東西,它分配參考。 所有語句都按順序執行。 賦值運算符將右側的值賦給左側的變量。 (這不是數學中的等價關系)。

  1. PolyNode next = iter1.next; 將當前節點的下一個節點的引用存儲到變量next中。
  2. iter1.next = poly1; 更新對當前節點的下一個節點的引用,以引用與引用poly1當前相同的 object 實例。 請注意, poly1null作為值開頭。
  3. poly1 = iter1; 更新poly1引用以引用與iter1引用相同的 object 實例。
  4. iter1 = next; 更新iter1引用以引用與next引用相同的 object 實例(舊的iter1.next引用,在它更改為poly1之前)。

我將嘗試將步驟繪制為美麗的 ASCII 藝術:

我們將從一個包含 3 個節點的簡單鏈表開始(即A.next == B; B.next == C; C.next == null; )。 我們從引用第一個節點的iter1和引用“無”的poly1開始(即null ):

A->B->C->null
^------------ iter1
         ^--- poly1 (of course, this there isn't any single null)
  1. PolyNode next = iter1.next;

     A->B->C->null ^------------ iter1 ^--------- next == iter1.next
  2. iter1.next = poly1;

     A->null | B->C->null ^------------------- iter1 ^---------------- iter1.next == poly1 (== null) ^--------- next
  3. poly1 = iter1;

     A->null | B->C->null ^------------------- iter1 == poly1 ^---------------- iter1.next == poly1 (== null) ^--------- next
  4. iter1 = next;

     A->null | B->C->null ^------------------- poly1 ^--------- next == iter1 ^------ iter1.next

while 循環的下一次迭代( iter1 != null ):

  1. PolyNode next = iter1.next;

     A->null | B->C->null ^------------------- poly1 ^--------- iter1 ^------ iter1.next == next
  2. iter1.next = poly1;

     B->A->null | C->null ^------------------- iter1 ^---------------- poly1 == iter1.next ^------ next
  3. poly1 = iter1;

     B->A->null | C->null ^------------------- iter1 == poly1 ^---------------- iter1.next ^------ next
  4. iter1 = next;

     B->A->null | C->null ^------------------- poly1 ^------ next == iter1

while 循環的下一次迭代( iter1 != null ):

  1. PolyNode next = iter1.next;

     B->A->null | C->null ^------------------- poly1 ^------ iter1 ^--- iter1.next == next
  2. iter1.next = poly1;

     C->B->A->null ^------------ iter1 ^--------- poly1 == iter1.next ^--- next
  3. poly1 = iter1;

     C->B->A->null ^------------ iter1 == poly1 ^--------- iter1.next ^--- next
  4. iter1 = next;

     C->B->A->null ^------------ poly1 ^--- next == iter1

iter1 != null現在為假,循環終止。 您的 function 通過將所有節點添加到新列表來有效地反轉鏈表; 我希望它的名字是reverse()

暫無
暫無

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

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