2
所以我無法嘗試從一個有兩個整數參數n和m的方法構建鏈表。參數n是鏈表的節點長度,m是節點列表中包含從0到m-1的隨機整數的參數。我需要從預定義的無法更改的Node類構建此鏈接列表,並將引用返回到鏈表中的第一個元素。我不知道如何遍歷while循環中的鏈表。如何使用具有兩個整數參數的方法構建鏈表並將引用返回到節點列表的第一個元素?
Node類
public class iNode{
public int item;
public iNode next;
public iNode(int i, iNode n){
item = i;
next = n;
}
public iNode(int i){
item = i;
next = null;
}
構建鏈表法
public static iNode list(int n, int m){
iNode first;
iNode newNode;
iNode last;
first = null;
while ()
{
newNode = new iNode(m, first.next);
if (m > 0){
newNode.item = m-1;
}
newNode.next = null;
if (first == null)
{
first = newNode;
last = newNode;
}
else
{
last.next = newNode;
last = newNode;
}
}
return first;
}
是的,但是我怎樣才能生成從m-1到0範圍的隨機數。 – Alan
剛剛添加到代碼中。 –