Java - 允许用户将节点添加到链接列表的方法

时间:2016-05-02 02:55:47

标签: java linked-list

我想要输出的内容:

水星金星地球火星木星土星天王星海王星

已找到第九颗行星!它是什么名字?:冥王星

你想炸毁哪个星球?:水星

金星地球火星木星土星天王星海王星冥王星

问题: 任何人都可以引导我找到如何修复注释掉的方法addNode的正确方向吗?是的,我知道这是完全错误的。

类NodePlanets.java

import java.util.Scanner;

public class NodePlanets
{
   public static void main(String[] args)
   {
      Node p, q, r;

      p = new Node("Mercury");
      q = p;

      r = new Node("Venus");
      q.next = r;
      q = r;

      r = new Node("Earth");
      q.next = r;
      q = r;

      r = new Node("Mars");
      q.next = r;
      q = r;

      r = new Node("Jupiter");
      q.next = r;
      q = r;

      r = new Node("Saturn");
      q.next = r;
      q = r;

      r = new Node("Uranus");
      q.next = r;
      q = r;

      r = new Node("Neptune");
      q.next = r;
      q = r;

      System.out.println(printLinkedListNodes(p));
      //addNode(p);
      System.out.println(printLinkedListNodes(p));
      removeNode(p);
      System.out.println(printLinkedListNodes(p));
   }//end main

   public static String printLinkedListNodes(Node nodePassed)
   {
      if(nodePassed.next == null)
         return nodePassed.data;
      else
         return nodePassed.data + " " + printLinkedListNodes(nodePassed.next);
   }//end method

/*

   public static void addNode(Node list, String userInput)
   {
      Scanner ui = new Scanner(System.in);
      System.out.println("A ninth planet has been found! What is it's name?");
      userInput = ui.nextLine();
      Node prev = null;
      Node curr = list;
      while (curr != null)
      {  
         prev = curr;
         curr = curr.getNext();
      }
      prev.setNext(new Node(userInput,null)); 
   }//end method   

*/

   public static Node removeNode(Node nodePassed)
   {
      Scanner ui = new Scanner(System.in);
      String userInput;
      System.out.println("Which Planet do you wish to blow up?");
      userInput = ui.nextLine();
      Node currentNode = nodePassed;
      Node temp;
      if(userInput.equalsIgnoreCase(currentNode.data))
      return currentNode.next; 
      while(currentNode.next!=null)
      {
         temp = currentNode.next.next;
         if(userInput.equalsIgnoreCase(currentNode.next.data))
         {
            currentNode.next = temp;
            return nodePassed;
         }
         currentNode = currentNode.next;
      }//endwhile
      return nodePassed;
   }//end method

}//end class

类Node.java

public class Node
{
   public String data;
   public  Node next;

   public Node()
   {
      data = "";
      next = null;
   }//end method

   public Node(String s)
   {
      data = s;
      next = null;
   }//end methodC

   public String toString()
   {
      return "Data:" + data;
   }//end method

}//end class

1 个答案:

答案 0 :(得分:0)

此方法绝对不应该是调用客户端的一部分,而应该是Node类的一部分。尽管如此,这应该会让你前进。

  public static void addNode(Node list)
       {
          Scanner ui = new Scanner(System.in);
          System.out.println("A ninth planet has been found! What is it's name?");
          String userInput = ui.nextLine();

          Node curr = list;
          while (cur.next != null)
          {  
             curr = curr.getNext();
          }
          curr.setNext(new Node(userInput, null));

       }//end method