我一直在盯着這一點的代碼,感覺像一個星期,不知道發生了什麼。我目前有一個LinkedList
需要添加500個元素。在我添加元素0的第一次運行中,它工作正常。它認爲這是第一個因素,創建一個新的頭,然後繼續前進。問題是,當代碼回來添加元素1時,頭部突然被重置爲空,所以它會移動並使另一個頭部踢出前一個頭部。在第三次運行時,代碼識別出有一個頭部,並嘗試添加元素2.但是,尺寸變量n
聲稱在LinkedList
中有2個元素,因爲第一個頭部消失,實際上只有一個元素。因此,尋找第二個元素時會引發NullPointerException
。那頭頭髮生了什麼?它通過add()
方法完成,但它回來時不在那裏。但它又回來了嗎?我的鏈接列表的頭突然消失/重置爲空
這裏的一切是走錯了具體方法:
public void add(int i, value_type value) {
n++;
System.out.println(head==null);
if (i < 0 || i >= size())
throw new IndexOutOfBoundsException();
System.out.println(i);
ListNode<value_type> tmp;
if (head == null) {
System.out.println("0");
head = new ListNode<value_type>(value, head);
} else {
System.out.println("1");
tmp = head;
for (int k=0; k < i-1; k++) {
System.out.println("For Loop Reached");
tmp = tmp.next;
}
if (i == 1) {
ListNode<value_type> u = new ListNode<value_type>(value);
tmp.next = u;
}
tmp.next = new ListNode<value_type>(value);
}
System.out.println(head==null);
System.out.println("");
}
而這裏的全班同學:
/**
*
*/
package data_structures;
import com.sun.corba.se.impl.orbutil.graph.Node;
/**
* @author
*
*/
/**
* The ListNode<value_type> is a helper class for your
* LinkedList<value_type> class. As its not intended for use
* outside the LinkeList class, we are keeping it simple -- the
* two properties will be access directly, instead of going through
* inspectors and mutators.
*
* DO NOT MODIFY THIST CLASS.
*
* @param <value_type> The type of object to be stored in the list.
*/
class ListNode<value_type> {
public value_type value;
public ListNode<value_type> next;
public ListNode(value_type v) {
value = v;
next = null;
}
public ListNode(value_type v, ListNode<value_type> n) {
value = v;
next = n;
}
}
/*
* We will implement this as a single linked list.
*/
public class LinkedList<value_type> extends Sequence<value_type> {
/**
* head will be the first node of the list -- or null if the list is empty
*/
private ListNode<value_type> head;
/**
* List constructor: must call the superclass constructor.
*/
public LinkedList() {
super();
head = null;
}
/* (non-Javadoc)
* @see data_structures.Sequence#get(int)
*/
@Override
public value_type get(int i) {
if (i < 0 || i >= size())
throw new IndexOutOfBoundsException();
ListNode<value_type> tmp = head;
for (int k=0; k < i; k++) {
tmp = tmp.next;
}
return tmp.value;
}
/* (non-Javadoc)
* @see data_structures.Sequence#set(int, java.lang.String)
*/
@Override
public value_type set(int i, value_type value) {
if (i < 0 || i >= size())
throw new IndexOutOfBoundsException();
if (head == null) {
throw new IndexOutOfBoundsException();
}
ListNode<value_type> tmp = head;
for (int k=0; k < i; k++) {
tmp = tmp.next;
}
if (tmp == null) {
throw new IndexOutOfBoundsException();
}
return tmp.value;
}
/* (non-Javadoc)
* @see data_structures.Sequence#add(int, java.lang.String)
*/
@Override
public void add(int i, value_type value) {
n++;
System.out.println(head==null);
if (i < 0 || i >= size())
throw new IndexOutOfBoundsException();
System.out.println(i);
ListNode<value_type> tmp;
if (head == null) {
System.out.println("0");
head = new ListNode<value_type>(value, head);
} else {
System.out.println("1");
tmp = head;
for (int k=0; k < i-1; k++) {
System.out.println("For Loop Reached");
tmp = tmp.next;
}
if (i == 1) {
ListNode<value_type> u = new ListNode<value_type>(value);
tmp.next = u;
}
tmp.next = new ListNode<value_type>(value);
}
System.out.println(head==null);
System.out.println("");
}
/* (non-Javadoc)
* @see data_structures.Sequence#remove(int)
*/
@Override
public value_type remove(int i) {
if (i < 0 || i >= size())
throw new IndexOutOfBoundsException();
ListNode<value_type> tmp = head;
for (int k=0; k < i; k++) {
tmp = tmp.next;
}
ListNode<value_type> a = tmp;
ListNode<value_type> b = tmp.next.next;
a.next = b;
return tmp.value;
}
/* (non-Javadoc)
* @see data_structures.Sequence#clear()
*/
@Override
public void clear() {
head = null;
}
}
能否請您與您的實際代碼共享該鏈表的使用情況如何? – mtyurt
你的意思是我項目中的其餘代碼? –
'remove'功能對於移除頭部並不安全,而且很麻煩。這可能是問題所在。我建議你看一下示例鏈表實現。 – mtyurt