「データ構造とアルゴリズム」に関する本を読んで、循環リンクリストの実装を求める割り当てがあります。これは学習の練習であり、私のコードは非常に高いレベルのものではないかもしれません。Javaで循環リンクリストを実装する方法は?
私の循環リンクリストの実装の主な考え方は、最後の要素を指すポインタを持つことです。新しい項目を追加するたびに、最後の項目のフィールド「次」がリフレッシュされ、新しく追加されたアイテム。
挿入方法はうまくいきますが、問題なく項目を追加できますが、何らかの理由でリストから項目を削除できません。ここで
は、「リンク」または「ノード」のコードです:
public class Link {
public long data;
public Link next;
public Link(long val) {
data = val;
next = null;
}
public void displayLink() {
System.out.print(data + " ");
}
} // end class
これは、作業を行ってクラスのコードで、バグはどこかここで明らかである:
public class CircularList {
Link first;
Link last;
public CircularList() {
first = null;
last = null;
}
public Link find(long key) {
Link current = first;
while(current.data != key) {
current = current.next;
}
return current;
} // end find
public Link delete() {
if(first.next == null)
last = null;
Link temp = first;
first = first.next;
return temp;
} // end delete
public boolean isEmpty() { return (first == null); }
public void insert(long val) {
Link newLink = new Link(val);
if(isEmpty())
last = newLink;
newLink.next = first;
first = newLink;
last.next = first;
} // end insert
public void displayAmount(int n) {
Link current = first;
while(n>0) {
current.displayLink();
current = current.next;
n--;
}
System.out.println("");
} // end displayAmount
} // end class
そして、メインのアプリコード:表示金額は一種の愚かに見える
public class App {
public static void main(String[] args) {
CircularList cl = new CircularList();
cl.insert(10);
cl.insert(20);
cl.insert(30);
cl.insert(40);
cl.displayAmount(6);
cl.delete();
cl.displayAmount(6);
}
} // end class
、私は無限ループとメートルを避けるために試してみました単純に機能する単純なものを用意してください。
あなたの質問は何ですか? –
リンクリストノードに以前のノードへの参照がないため、削除が不可能です。最後の要素を最初のものと最後のものの両方を参照するようにしたいとします。つまり、次と前の両方が必要です。これらを使用すると、任意の要素を取り、現在の要素の前と次を取得し、前の要素と次の要素を接続して、削除する要素を効果的に切り捨てることができます。 –
@G_V 'delete()'メソッドは、最初の要素を常に削除しようとします。その前の要素が 'last'であるため、OKです。任意の要素を削除したい場合は二重にリンクする必要がありますが、 'first'を削除するだけであれば、その必要はありません。 –