私はコンピュータサイエンスのクラスのプロジェクトを持っており、1つの方法を除いてすべてを行っています。削除メソッド。基本的に私はユーザー入力からリンクリストを作成しており、すべてのノードを削除できるようにする必要があり(これは実行されます)、指定された単一のノードを削除できる必要があります。したがって、ノードのリストを検索して、削除するノードを見つけて削除する必要があります。助けることができるものは何でもありがたいです。あなたが解決策を持っているなら、私が学び、問題を解決しようとしているので、説明を提供してください。
GUIは必要ないと思うので、ここでは説明しませんが、ここにノードクラスがあります。
public class MagazineList {
private MagazineNode list;
public MagazineList(){
list = null;
}
public void add(Magazine mag){
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if(list == null) {
list = node;
}
else {
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public void insert(Magazine mag) {
MagazineNode node = new MagazineNode (mag);
// make the new first node point to the current root
node.next=list;
// update the root to the new first node
list=node;
}
public void deleteAll() {
if(list == null) {
}
else {
list = null;
}
}
public void delete(Magazine mag) {
//Delete Method Goes Here
}
public String toString(){
String result = " ";
MagazineNode current = list;
while (current != null){
result += current.magazine + "\n";
current = current.next;
}
return result;
}
private class MagazineNode {
public Magazine magazine;
public MagazineNode next;
public MagazineNode(Magazine mag){
magazine = mag;
next = null;
}
}
}
アップデート
これが私がまとめた方法であり、最初の部分を通過してwhileループに入り、リスト内の同じ項目を認識することはありません。inputメソッドとdeleteメソッドにまったく同じものを使用しましたが、認識されません。どんな助けでも大歓迎です。
public void delete (Magazine mag) {
MagazineNode current = list;
MagazineNode before;
before = current;
if(current.equals(mag)){
before.next = current;
System.out.println("Hello");
}
while ((current = current.next)!= null){
before = current.next;
System.out.println("Hello Red");
if(current.equals(mag)){
current = before;
System.out.println("Hello Blue");
}
}
}