sorting - Ordering java linked list alphabetically (dictionary-like) -


i've been working hours trying order linked list of strings alphabetically (dictionary-like). given string lowercase only. example, input of: "hello name albert" sorted in list as: node 1: albert, node 2: hello, node 3: is, etc..

my code far reads string example above , insert nodes - unordered.

i've searched in web ways sort linked list alphabetically performance, , found merge sort can usefull. i've changed merge sort work string using compareto() code returns nullpointerexception error in following line:

if(firstlist._word.compareto(secondlist._word) < 0){ 

i'm looking fix following code or way sorting linked list alphabetically (without collection.sort)

my full code (after trying add merge sort work code):

public class textlist {     public wordnode _head;      public textlist()      {     _head = null;     }      public textlist (string text)     {         this._head = new wordnode();         int lastindex = 0;         boolean foundspace = false;         string newstring;         wordnode prev,next;         if (text.length() == 0) {             this._head._word = null;             this._head._next = null;         }         else {         (int i=0;i<text.length();i++)         {                 if (text.charat(i) == ' ') {                newstring = text.substring(lastindex,i);                insertnode(newstring);                // update indexes                 lastindex = i;                 // set true when string has space                 foundspace = true;           }     }         if (!foundspace) {             //if didnt find space, set given word             _head.setword(text);             _head.setnext(null);          }         else {             //insert last word             string laststring = text.substring(lastindex,text.length());             wordnode lastnode = new wordnode(_head._word,_head._next);             _head.setnext(new wordnode(laststring,lastnode));          }          sortlist(_head);      } }        private void insertnode(string word)       {       //create new node , put curret node in       wordnode newword = new wordnode(_head._word,_head.getnext());       //set new information in head       _head._word = word;       _head.setnext(newword);     }  private wordnode sortlist(wordnode start) {         if (start == null || start._next == null) return start;         wordnode fast = start;         wordnode slow = start;         // in middle of list :         while (fast._next!= null && fast._next._next !=null){             slow = slow._next; fast = fast._next._next;         }         fast = slow._next;         slow._next=null;         return mergesortedlist(sortlist(start),sortlist(fast));         }          private wordnode mergesortedlist(wordnode firstlist,wordnode secondlist){             wordnode returnnode = new wordnode("",null);             wordnode trackingpointer = returnnode;             while(firstlist!=null && secondlist!=null){                 if(firstlist._word.compareto(secondlist._word) < 0){                     trackingpointer._next = firstlist; firstlist=firstlist._next;                 }                 else {                     trackingpointer._next = secondlist; secondlist=secondlist._next                     ;}                 trackingpointer = trackingpointer._next;             }             if (firstlist!=null) trackingpointer._next = firstlist;             else if (secondlist!=null) trackingpointer._next = secondlist;             return returnnode._next;             }          public string tostring() {             string result = "";             while(_head.getnext() != null){                 _head = _head.getnext();                 result += _head._word + ", ";             }             return "list: " + result; }       public static void main(string[] args) {         textlist str = new textlist("a b c d e b");         system.out.println(str.tostring());     }  } 

in past have made method sort strings alphabetically in array school hw, umm here is:

    private void sortstringsalphabetically(){     (int = 0; < names.length; all++) {         (int = + 1; < names.length; i++) {             if (names[all].compareto(names[i]) > 0) {                 string tmp = names[i];                 names[i] = names[all];                 names[all] = tmp;             }         }     } } 

this piece of code works arrays , array of names. can tweak work list, simple if consider wide range of methods in list interface , it's implementations.

cheers.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -