开发者

Inserting Nodes Into a Linked List

开发者 https://www.devze.com 2023-01-18 00:40 出处:网络
I\'m fairly new to java so sorry in advance if this is terrible. Basically I\'m suppose to insert a number say the linked list is [10 30 50 70] I want to insert 40 so I type next and then next again a

I'm fairly new to java so sorry in advance if this is terrible. Basically I'm suppose to insert a number say the linked list is [10 30 50 70] I want to insert 40 so I type next and then next again and now I'm at 50. I want to insert before but when I type before and then 40 it just returns [10 30 50 70] total = 4 and current is 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;
}

Here is the code that is calling these two methods and providing the list. Any suggestions?

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.开发者_JAVA技巧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);
   }

}  


Since this is homework, I'll lead you to the right direction.

Your insertAfter method is wrong.

The first line you create a newnode. The second line you overwrite this newnode with current.

Start with these errors.

The best way is to draw a picture with your links. Think exactly what you need to do to put your new node into the list.

0

精彩评论

暂无评论...
验证码 换一张
取 消