2016-05-04 15 views
1

誰かが理由を説明できますか? temp.setNext(current.getNext());私はあなたが(temp変数によって参照)、新しいノードは、whileループが行われた後current.getNext()によって参照されるindex位置にノードの前に挿入されるようにしたいリンクリストの設定参照

public void add(Object data, int index) 
     // post: inserts the specified element at the specified position in this list. 
     { 
       Node temp = new Node(data); 
       Node current = head; 
       // crawl to the requested index or the last element in the list, 
       // whichever comes first 
       for(int i = 1; i < index && current.getNext() != null; i++) 
       { 
         current = current.getNext(); 
       } 
       // set the new node's next-node reference to this node's next-node reference 
       temp.setNext(current.getNext()); 
       // now set this node's next-node reference to the new node 
       current.setNext(temp); 
       listCount++;// increment the number of elements variable 
     } 

答えて

0

を理解していないです使用されてきました。

は、したがって、あなたは、最初に(temp.setNext(current.getNext());current.getNext()tempの次のノードを設定し、(current.setNext(temp);tempcurrentの次のノードを設定します。これにより、と元のcurrent.getNext()の間にtempが配置されます。

前:

... -> current -> current.getNext() -> ... 

後:説明のため

... -> current -> temp -> current.getNext() (the original current.getNext()) -> ... 
+0

感謝!!しかし、オリジナルのcurrent.getnext() – Sri