我的目標是製作一個鏈接列表,其中每個鏈接都是一個字符。我希望它將一個字符串作爲參數,取第一個字母並將其轉換爲一個char,然後將其餘的字符串傳遞到下一個鏈接,直到整個字符串被存儲爲止。這是我迄今爲止,雖然我不確定它的哪些部分是正確的或不正確的。我查了一堆例子,這似乎是默認設置。我很難實現我的鏈表類
public class linkedChar{
char data;
linkedChar head;
linkedChar next;
//This is the empty link constructor
public linkedChar(){
next = null;
}
//This is the constructor that takes a string
public linkedChar(String input){
if(input.length() > 0){
data = input.charAt(0);
next = new linkedChar(input.substring(1));
}
}
}
該代碼編譯但它不能與我的其他操作方法一起工作。例如,我的長度方法。
public int length(){
int length = 0;
linkedChar curr = head;
while(curr != null){
curr = curr.next;
length++;
}
return length;
}
使用時,返回的長度始終爲0。我不知道這部分代碼有錯誤,我不知道如何解決它。任何幫助將是偉大的,謝謝。
哦,呵呵,這是有道理的。謝謝!我能解決這個問題。 – rimbleysucksatjava