将节点插入链接列表

时间:2010-09-24 20:17:12

标签: java

我对java很新,所以如果这很糟糕,请提前对不起。基本上我想插入一个数字说链接列表是[10 30 50 70]我想插入40所以我输入下一个然后再次下一个然后我现在是50.我想插入之前但是当我键入之前和之后40只返回[10 30 50 70]总数= 4且电流为50。

public void insertAfter(long dd)  {   
    Node newNode = new Node(dd);   // insert a new node after current node; 
    newNode = current;
    current = previous;
    if (current != null) {     // if current is not null, don't change it; otherwise set current to new node.
       return;

    } 
    else {
     newNode = current;
    }
}

public void insertBefore(long dd) {   
    Node newNode = new Node(dd);  // insert a new node before current node, always set current to the new node
    current = previous; // to be implemented
    newNode = current;
}

以下是调用这两种方法并提供列表的代码。有什么建议吗?

package hw4;

import java.io.*;                 // for I/O

class TestLinkList {

   public static void main(String[] args) throws IOException
      {
      LinkList theList = new LinkList();          // new list

      theList.insertFirst(70);
      theList.insertFirst(50);
      theList.insertFirst(30);
      theList.insertFirst(10);
      theList.reset();

      while(true) {
         System.out.print("Enter first letter of reset, ");
         System.out.print("next, get, before, after, delete, exit: ");
         System.out.flush();
         int choice = getChar();         // get user's option
         long value;
         switch(choice)
            { 
            case 'r':                    // reset (to first)
               theList.reset();
               theList.displayList();
               break;
            case 'e':                    // exit the while loop
                break;
            case 'n':                    // advance to next item
               if( theList.getCurrent() != null ) {
                  theList.nextLink();
                  theList.displayList();
               } else
                  System.out.println("Can't go to next link");
               break;
            case 'g':                    // get current item
               if( theList.getCurrent() != null ) {
                  value = theList.getCurrent().dData;
                  System.out.println("Returned " + value);
                  }
               else
                  System.out.println("List is empty");
               break;
            case 'b':                    // insert before current
               System.out.print("Enter value to insert: ");
               System.out.flush();
               value = getInt();
               theList.insertBefore(value);
               theList.displayList();
               break;
            case 'a':                    // insert after current
               System.out.print("Enter value to insert: ");
               System.out.flush();
               value = getInt();
               theList.insertAfter(value);
               theList.displayList();
               break;
            case 'd':                    // delete current item
               if( theList.getCurrent() != null ) {
                  value = theList.deleteCurrent();
                  System.out.println("Deleted " + value);
                  theList.displayList();
               } else
                  System.out.println("Can't delete");
               break;
            default:
               System.out.println("Invalid entry");
            }  // end switch

            if (choice == 'e') break;
         }  // end while

   }  // end main()

   public static String getString() throws IOException {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
   }

   public static char getChar() throws IOException {
      String s = getString();
      return s.charAt(0);
   }

   public static int getInt() throws IOException {
      String s = getString();
      return Integer.parseInt(s);
   }

}  

1 个答案:

答案 0 :(得分:2)

由于这是家庭作业,我会引导你走向正确的方向。

您的insertAfter方法错误。

您创建newnode的第一行。 第二行用current覆盖这个newnode。

从这些错误开始。

最好的方法是用链接绘制图片。仔细想想将新节点放入列表中需要做些什么。