2013-04-12 57 views
-3
public CarList(CarList cl) 
{ 
    if(cl == null) throw new NullPointerException(); 
    if (cl.head == null) 
     head = null; 
    else 
    { 
     // Notice that you cannot issue head = cl.head; otherwise both heads will point at the passed list; 

     head = null; 
     // Now create and copy all the nodes in the list 
     CarNode temp, temp2, temp3; 
     temp = cl.head; 
     temp2 = null; 
     temp3 = null; 


     while(temp != null) 
     { 
      if (temp2 == null)  // The case for the first node 
      { 
       temp2 = new CarNode(temp.getCar(), null); 
       head = temp2; 
      } 
      else 
      { 
       temp3 = new CarNode(temp.getCar(), null); 
       temp2.setNext(temp3); 
       temp2 = temp3; 
      } 

      temp = temp.getNext(); 
     } 
     // Avoid privacy leak; set all temporary pointers to null 
     temp = temp2 = temp3 = null; 
    } 
} 

我不太明白環路做什麼......我不能解析的代碼。隱私權是否是由於臨時變量包含地址這一事實造成的?爲什麼我們在循環中指定一個值,如果它沒有被使用?

+0

呃,'head'使用。看到這一行:'head = temp2;' – jrd1

+0

它從來沒有用在_ code_;然而,'head'是CarList類的一個成員字段,並且(採用有教養的猜測)在課程的其他地方使用。 –

回答

1

它用於:

head = temp2; 

你的標籤明確指定是怎麼回事。卡洛斯是表示單鏈接列表,其中所述列表中的每個元素在一個CarNode,其中包含該元素,加上在列表中的下一個元素的鏈接包裹起來的類。

的「頭」變量指向列表中的第一個CarNode。有問題的while循環,而它的變量名是有問題的,簡單的拷貝參數列表進入一個被實例化,使得每一個元素的新CarNode。通過一個4元素的例子,你會看到它在做什麼。

它會對你很好地查找「Lisp的CAR功能」的詳細信息。 This page也有一些關於它的信息。

相關問題