0

I have a singly linked list in which I'd like to add alphabetically based on last name. This is my second linked list I have built so I'm new to it.

My plan was to enter a full name, then split it up when a space occurred. I accomplished this by following code:

  String y=newNode.getName();
    String [] subCurrent=y.split(" ");
    String last=subCurrent[subCurrent.length-1];

       System.out.println(last);

I would then compare two last names, one from newNode and another from Node current in a while loop:

     Node current=head;
        while (current!=null){

           String currentName= current.getName();
           String [] subCurrent1=currentName.split(" ");
           String last2=subCurrent1[subCurrent1.length-1];
           System.out.println(last2.compareTo(last)); 

If the compare came back greater than 0, Id insert newNode before the current node in the while loop:

             if( last2.compareTo(last)>0){ 
             // if number is larger than 0, put newNode infront of current.

             newNode.setLink(current.getLink());  
             current.setLink(newNode);
         }

If not, Id assign newNode to tail.

My question is, is my theory about adding alphabetically relatively correct? My list is not alphabetical as I add nodes. For instance, the first name I add is "joe z".

Then I add "john c".

john c should be put infront of joe z, however, my console prints out

joey@z.net 555-5555 joey z

john@z.net 555-6666 john c

I then add the name "paul a"

the console after that is:

joey@z.net 555-5555 joey z

paul@z.net 555-5777 paul a

john c gets deleted.

My whole add method:

 public void addAfter( String email, String name, String number){

    Node newNode= new Node( email, name, number ,null); 
    index++;
    newNode.setEmail(email);
    newNode.setName(name);
    newNode.setNumber(number);
    if (head==null){  
        newNode.setLink(null);
        head=newNode;
    }

    String y=newNode.getName();
    String [] subCurrent=y.split(" ");
    String last=subCurrent[subCurrent.length-1];

      Node current=head;
      while (current!=null){

        String currentName= current.getName();
        String [] subCurrent1=currentName.split(" ");
        String last2=subCurrent1[subCurrent1.length-1];

        // comparing current getName (last2) to newNode's (last) getName.

        if( last2.compareTo(last)>0){ // if number is larger than 0, put newNode infront of current.

             newNode.setLink(current.getLink());  
             current.setLink(newNode);

         }

         if (last2.compareTo(last)<0){ // if number is less than 0, assign newNode as tail
             newNode.setLink(null);
             tail=newNode;

         } 
         current=current.getLink();
     }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Use this to choose the best collection for the job: http://stackoverflow.com/questions/21974361/what-java-collection-should-i-use – aliteralmind Mar 02 '14 at 19:15
  • 1
    As the questioner stated in the title, this is homework and is required to implement a Collection-like class. – Ryan Ransford Mar 02 '14 at 19:18

0 Answers0