我在閱讀有關循環鏈表的內容。這是一個代碼,我不明白它是如何工作的。元素在循環鏈表中的索引
public int elementAt(int index){
if(index>size){
return -1;
}
Node n = head;
while(index-1!=0){ // this line is unclear for me
n=n.next;
index--;
}
return n.data;
}
我會寫同樣的代碼,但以這樣的方式
public int elementAt(int index){
if(index>size){
return -1;
}
Node n = head;
while(n.size != index){ // here is my change in the code
n=n.next;
}
return n.data;
}
這裏是整個代碼:http://algorithms.tutorialhorizon.com/circular-linked-list-complete-implementation/
我在第二個代碼權做什麼?
謝謝
爲什麼不自己檢查/使用調試器? – Idos
'n.size'會是什麼? – Thomas
這裏是整個代碼http://algorithms.tutorialhorizon.com/circular-linked-list-complete-implementation/謝謝 – Joe