質問

基本的に、このプログラムは、幹部の「円卓会議」を作成することになっており、変更できない議長がいます。私はちょっと自分が何をしているのかをほぼ知っていて、幹部を挿入して削除する方法の途中ですが、コードをテストして、それがどのように進んでいるかを確認しようとしました。情報。また、executiveListのremoverbycorporationメソッドをどのように進めるかはまったくわかりません。この方法がほぼすべて正しくないことはほぼ確信しており、このような円形の二重リンクリストのノードを削除する方法ではありません。

*印刷方法を手伝う必要はありません。私はまだそれらに到達していません。

TL; DR:1)なぜすぐにクラッシュするのですか? 2)RemoveByCorporation方法がまったく間違っていると確信しています。もしそうなら、それを修正する方法についての提案や助けはありますか?

他のクラスが私に知らせてくれるのを見たいなら、私が問題を抱えている2つのクラスは次のとおりです。

ファーストクラス

public class ExecutiveList {

private ExecutiveNode chair;
ExecutiveNode cursor;

public ExecutiveList() {

}

public ExecutiveList (Executive chairperson) {

    chair.setExecutive(chairperson);
    chair.left = chair;
    chair.right = chair;
}

public void insertLeftOfChair(Executive exec) {

    ExecutiveNode newExec = new ExecutiveNode();
    newExec.setExecutive(exec);
    chair.setLeft(newExec);

}

public boolean insertRightOfExec (Executive exec, String target) {
    cursor = chair;
    ExecutiveNode newExec = new ExecutiveNode();

    do { cursor = cursor.getLeft();
    if (cursor.getExecutive().equals(exec)) {
        newExec.setExecutive(exec);
        cursor.getRight().setLeft(newExec);
        newExec.setLeft(cursor);
        newExec.setRight(cursor.getRight());
        cursor.setRight(newExec);
        return true;
    }
    else {
        return false;
    }
    }       while (cursor.getExecutive().getExecutiveName() != target);


}

public boolean insertLeftOfExec (Executive exec, String target) {
    cursor = chair;
    ExecutiveNode newExec = new ExecutiveNode();
    do { cursor = cursor.getLeft();
        if (cursor.getExecutive().equals(exec)) {
            newExec.setExecutive(exec);
            cursor.getLeft().setRight(newExec);
            newExec.setRight(cursor);
            newExec.setLeft(cursor.getRight());
            cursor.setLeft(newExec);

            return true;
        }
        else {
            return false;
        }
    }       while (cursor.getExecutive().getExecutiveName() != target);
}

public boolean removeTargetExec(String name) {
    if (chair.equals(name)) {
        return false;
    }
    else {
        return false;
    }
}

public int removeByCorporation(String corporation) {
    int removed = 0;
    cursor = chair;
    do {
        if (cursor.getExecutive().getCompanyName().equals(corporation)) {
            cursor.setExecutive(null);
            cursor.getLeft();
            removed = removed + 1;
        }
    } while (removed > 0);

    return removed;
}

public void printByCorporation(String corporation) {

}

public void printAllClockwise() {

}

public void printAllCounterClockwise() {

}
    }

セカンドクラス

import java.util.Scanner;

    public class MeetingManager {
public static void main(String[] args) {

    // scanner to read the users input
    Scanner input = new Scanner(System.in);

    // strings to pass information about the chairperson
    String chairpersonName;
    String chairpersonCompany;

    // strings to pass information about executives other
    // than the chairperson
    String execName;
    String execCompany;
    String target;

    // holds information on whether on not an operation
    // was successful and how many executives were removed
    // for the remove by corporation command.
    boolean success;
    int numRemoved = 0;

    // prompts the user for information about the chairperson
    // and sets it to an executive object name chairperson
    System.out.println("Enter the name of the chairperson: ");
    chairpersonName = input.next();
    if (chairpersonName.length() < 1) {
        System.out.println("Please enter a full name");
    }
    System.out.println("Enter the company of the chairperson: ");
    chairpersonCompany = input.next();
    if (chairpersonCompany.length() < 1) {
        System.out.println("Please enter a full name");
    }
    Executive chairperson = new Executive(chairpersonName, chairpersonCompany);

    // creates a new ExecutiveList object and passes information
    // about the chairperson
    ExecutiveList list = new ExecutiveList(chairperson);

    // for loop to repeatedly print the menu and take instructions
    // from the user until they choose to exit.
    for (int i = 1; i > 0; i++) {
        ShowMenu();
        String option = input.next();

        // error message for improper input
        if (option.length() > 3) {
            System.out.println("You can only enter one option");
        }

        // insert left of chairperson
        else if (option.toUpperCase().equals("ILC")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            list.insertLeftOfChair(newGuy);
            System.out.println("Insertion successful.");
        }

        // insert left of executive
        else if (option.toUpperCase().equals("ILE")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            System.out.println("Enter the name of the target executive: ");
            target = input.next();

            success = list.insertLeftOfExec(newGuy, target);
            if (success == true) {
                System.out.println("Insertion successful.");
            }
            else {
                System.out.println("The executive could not be inserted.");
            }
        }


        // insert right of executive
        else if (option.toUpperCase().equals("IRE")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            System.out.println("Enter the name of the target executive: ");
            target = input.next();

            success = list.insertRightOfExec(newGuy, target);
            if (success) {
                System.out.println("Insertion successful.");
            }
            else {
                System.out.println("The executive could not be inserted.");
            }
        }

        // remove target executive
        else if (option.toUpperCase().equals("RTE")) {
            System.out.println("Enter the name of the executive to remove: ");
            execName = input.next();
            success = list.removeTargetExec(execName);

            if (execName.equals(chairpersonCompany))
                list.removeTargetExec(execName);
            if (success) {
                System.out.println(execName + " has been removed from the meeting.");
            }
            else {
                System.out.println(execName + " could not be found.");
            }
        }

        // remove by corporation
        else if (option.toUpperCase().equals("RBC")) {
            System.out.println("Enter the name of the corporation to remove: ");
            execCompany = input.next();
            numRemoved = list.removeByCorporation(execCompany);
            if (execCompany.equals(chairperson.getCompanyName())) {
                System.out.println("Invalid command: cannot remove all employees from the chairperson's corporation");
            }
            else if (numRemoved < 1) {
                System.out.println("That corporation could not be found and no executives were removed.");
            }
            else {
                System.out.println(numRemoved + " executive(s) from " + execCompany + " have been removed from the meeting.");
            }
        }

        // prints by corporation
        else if (option.toUpperCase().equals("PBC")) {
            System.out.println("Enter the name of a corporation to display: ");
            execCompany = input.next();
            list.printByCorporation(execCompany);
        }

        // prints all counter-clockwise
        else if (option.toUpperCase().equals("PCC")) {

            list.printAllCounterClockwise();
        }

        // prints all clockwise
        else if (option.toUpperCase().equals("PCL")) {

            list.printAllClockwise();
        }

        else if (option.toUpperCase().equals("EXT")) {
            System.out.println("Terminating program...");
            break;
        }

        // Error message
        else {
            System.out.println("Please select a valid option.");
        }

    }

}



// displays menu and prompts user for input
public static void ShowMenu() {
    System.out.println("\nILC) Insert an executive to the left of the chairperson\nILE) Insert an executive to the left of a given executive\nIRE) Insert an executive to the right of a given executive\nRTE) Remove Target Executive");
    System.out.println("RBC) Remove By Corporation\nPBC) Print By Corporation\nPCC) Print all in counter-clockwise order\nPCL) Print all in clockwise order\nEXT) Exit the program\n\nSelect a menu option: ");
}

}

最後に、あらゆる種類の提案やアドバイス、または実際の助けをあらゆる方法で提供してくれた人に感謝します。生徒が「私のために宿題をする」ように頼んでいると思っているので、何らかの理由で宿題の質問を見ると人々が怒っていることを知っていますが、それは私がやっていることではありません。私は単にアドバイスやヒントが好きです、私はあなたに私のために空白を埋めてすべてを修正するようにあなたに求めているのではありません(私がそれに反対するというわけではありません:P)。ありがとう。

役に立ちましたか?

解決

removeByCorporationメソッドでは、エグゼクティブをNULLに設定するだけですが、これが二重リンクリストであると考えると、二重リンクされたリストが壊れないように、前と次のエグゼクティブの参照を設定する必要があるとは思わないでください。

他のヒント

トリックは、すべての操作で、変更されているアイテムとそれを参照する他の2つを更新することを確認することです(二重リンクリストで非常に便利に、2つは2つのIT参照を参照しています)。

それぞれの方法を確認し、それぞれに変更ごとに4つのフィールドを更新していることを確認してください - 被写体の2つ、およびそれぞれが被写体からリンクされている2つに1つを確認してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top