私はこのコードを一週間のように感じていて、何が起こっているのか分からない。私は現在、LinkedList
に500要素を追加する必要があります。私が要素0を追加している最初の実行では、正常に動作します。それはそれが最初の要素であることを見て、新しい頭を作り、動きます。問題は、コードが要素1を追加するために周りに戻ってきたときに、ヘッドが突然nullにリセットされたため、別のヘッドを作成して前のヘッドを追い出すことです。 3回目の実行では、コードは頭が存在することを認識し、要素2を追加しようとします。n
は、LinkedList
に2つの要素があると主張します。したがって、その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