我目前正在對我的任務是實現鏈表數據結構,我已經完成了,只不過它不能正常工作,這是我要和測試它的輸入代碼我的代碼是「1,2,3,4,5」,但是隻輸出「1」而不是所有的值。鏈表,不增加值正確
這裏是我的代碼:
// Main Method Functions
private static LinkedList createLinkedList(int[] values) {
LinkedList list;
list = new LinkedList();
for (int i = 0; i < values.length; i++) {
list.add(values[i]);
}
return list;
}
private static void printList(LinkedList list) {
Node currentNode = list.getHead();
while(currentNode != null) {
System.out.println(currentNode.getint());
currentNode = currentNode.getNextNode();
}
}
// LinkedList class functions
public void add(int value) {
Node newNode = new Node(value);
Node currentNode;
if(head == null) {
head = newNode;
} else {
currentNode = newNode;
while(currentNode.getNextNode() != null) {
currentNode = currentNode.getNextNode();
}
currentNode.setNextNode(newNode);
}
size++;
}
任何人都可以指出我在做什麼錯?如果您需要添加其他功能,請告知我,謝謝。
編輯:這說明添加值的函數:
private static void processLinkedList() {
int[] values = new int[] {1,2,3,4,5};
LinkedList list = createLinkedList(values);
printList(list);
System.out.println(list);
}
「不工作」是一個足夠的問題描述。請詳細說明。 – 2013-04-04 23:36:29
你需要更具體。它如何不正常工作?你做了什麼來測試它?你認爲這個問題在哪裏? – BevynQ 2013-04-04 23:37:31
對不起,我測試的輸入值是「1,2,3,4,5」,但只輸出「1」。 – user123 2013-04-04 23:37:49