2012-02-17 66 views
2

基本上,程序应该创建一个高管的“圆桌会议”,主席不能更改。我有点差不多了解自己在做什么,而且我正在通过我的插入和删除管理人员的方法的一半,但我只是试图测试我的代码,看看它是如何发生的,只要输入主持人就会发生错误信息。另外,我并不确定如何去执行ExecutiveList中的removeByCorporation方法。我几乎肯定方法几乎都是不正确的,我只是没有如何去除这样的循环双向链表中的节点。Java中的循环双向链接列表程序(作业帮助)

*没有必要帮助我的印刷方法,我只是还没有得到他们。

tl; dr: 1)为什么它马上崩溃? 2)我很确定我的removeByCorporation方法是完全错误的。如果是这样,有什么建议或帮助如何解决它?

以下是我遇到的两个课程,如果您希望看到其他课程让我知道,我会发布它们,但它们是99%的获取者和设置者。

FIRST CLASS

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)。谢谢。

+0

这将是更好的给我们,这是获得抛出说它不是异常崩溃 – Rocky 2012-02-17 04:38:08

+0

控制台: 输入主席的名称: DERP 输入公司主席: HERP 异常在线程“ main“java.lang.NullPointerException \t at ExecutiveList。 (ExecutiveList.java:23) \t at MeetingManager.main(MeetingManager.java:41) – Dim 2012-02-17 04:55:38

回答

1

在removeByCorporation方法中,您只需将执行程序设置为null,但考虑到这是双向链接列表,您不认为需要设置上一个和下一个执行程序的引用,以便双向链接列表不会不会中断。

0

诀窍在于确保在每一次操作中更新被更改的项目以及引用它的其他两个项目(很容易在双向链接列表中,引用它的两个也是它引用的两个项目) 。

检查你的每一个方法,并确保在每一个方法中你每更新4个字段 - 两个在主题中,两个从主题链接。