1

私はコンピュータサイエンスのクラスのプロジェクトを持っており、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");
    }
}
 }
4

3 に答える 3

5

スプーンで餌を与えずに答えを。削除は、チェーンの1つのリンクを削除するのと少し似ています。リンクを切り取り、2つの(新しい)端を結合します。

したがって、「B」を削除すると、変更することになります

A --> B --> C --> D

これに

A --> C --> D


擬似コードでは、アルゴリズムは次のようになります。

  • 最初のノードでアルゴリズムを開始します
  • 削除したいものか確認してください
  • そうでない場合は、次のノードに移動して再度確認します(前の手順に戻ります)
  • その場合、前のノードの次のノードをこのノードの次のノードにします
  • このノードから次のノードへの参照を削除します
于 2012-11-30T02:50:28.787 に答える
3
public void delete (Magazine mag) {
    MagazineNode current = this.list;
    MagazineNode before;

    //if is the first element
    if (current.equals(mag)) {
        this.list = current.next;
        return;     //ending the method
    }


    before = current;

    //while there are elements in the list
    while ((current = current.next) != null) {

        //if is the current element
        if (current.equals(mag)) {
            before.next = current.next;
            return;     //endind the method 
        }

        before = current;
    }

    //it isnt in the list
}

コメントは何が起こっているのかを説明する必要があります

于 2012-11-30T03:06:56.750 に答える
2

あなたがする必要があるのはあなたがどこにいるかを追跡しながら、リストを検索することです。削除するノードが目の前にある場合は、現在のノードの「次」を削除するノードの次のノードに設定します。

for(Node current = list; current.next() != null; current = current.next()){
   if(current.next().magazine().equals(toDelete)) current.setNext(current.next().next());
}

そんな感じ。お役に立てば幸いです。

于 2012-11-30T02:52:44.827 に答える