2010-09-24 4 views
3

Je suis assez nouveau à Java si désolé à l'avance si c'est terrible. Fondamentalement, je suppose d'insérer un certain nombre dire la liste liée est [10 30 50 70] Je veux insérer 40, donc je tape à côté, puis à nouveau et maintenant je suis à 50. Je veux insérer avant, mais quand je tape avant et 40, il retourne juste [10 30 50 70] total = 4 et le courant est 50.Insertion de nœuds dans une liste chaînée

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; 
} 

Voici le code qui appelle ces deux méthodes et fournir la liste. Aucune suggestion?

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); 
    } 

} 
+0

Serait bon si vous postez le code pour 'LinkList' et' displayList() 'aussi, ce qui ne va pas aussi pourrait dépendre de ceux-ci. – schnaader

Répondre

2

Puisque c'est des devoirs, je vais vous conduire à la bonne direction.

Votre méthode insertAfter est incorrecte.

La première ligne crée un nouveau nœud. La deuxième ligne remplace ce nouveau nœud par le courant.

Commencez par ces erreurs.

La meilleure façon est de dessiner une image avec vos liens. Pensez exactement à ce que vous devez faire pour mettre votre nouveau noeud dans la liste.

Questions connexes