java - Add elements between nodes Ordered LinkedList -


i queried search engine on site, did not see matched looking for, hope question has not been answered elsewhere. trying perfect add method ordered linkedlist. rest of program runs fine, , while list prints out specified in test harness, want names sorted.

my output after several calls add , remove harness:

sue, bill, michael, someguy, michael, carl, steve, carl, sue sue, bill, someguy, michael, steve, carl, sue sue, bill, someguy, michael, steve, carl, sue, sue, bill 

what want this: sue, sue, bill, michael, michael, carl, carl, steve, etc...

my add method:

 public boolean add(comparable obj) {     orderedlistnode newnode = new orderedlistnode(obj, null, null);     if( tail == null) {     tail = newnode;     head = tail;     modcount++;     return true;      }     if(((comparable)(head.theitem)).equals(obj)){         tail.previous = newnode;         modcount++;         return true;     }else{       tail.previous.next = newnode;     newnode.previous = tail.previous;     newnode.next = tail;     tail.previous = newnode;     modcount++;     return true;     } } 

entire code, since asked for:

package datastructures;   public class orderedlinkedlist {  /**************************************************************************  * constants  *************************************************************************/  /** return value unsuccessful searches */ private static final orderedlistnode not_found = null;   /**************************************************************************  * attributes  *************************************************************************/  /** current number of items in list */ private int thesize;  /** reference list header node */ private orderedlistnode head;  /** reference list tail node */ private orderedlistnode tail;  /** current number of modifications list */ private int modcount;   /**************************************************************************  * constructors  *************************************************************************/   /**  *  create instance of orderedlinkedlist.  *  */ public orderedlinkedlist() {     // empty orderedlinkedlist     clear(); }   /**************************************************************************  * methods  *************************************************************************/   /*  *  add specified item orderedlinkedlist.  *  *  @param  obj     item added  */ public boolean add(comparable obj) {     orderedlistnode newnode = new orderedlistnode(obj, null, null);     if( tail == null) {     tail = newnode;     head = tail;     modcount++;     return true;      }     if(((comparable)(head.theitem)).compareto(obj) > 0){         ////////////////////////////////////////         //here problem lies, believe         /////////////////////////////////////////         modcount++;         return true;     }else{       tail.previous.next = newnode;     newnode.previous = tail.previous;     newnode.next = tail;     tail.previous = newnode;     modcount++;     return true;     } }  /*  *  remove first occurrence of specified item orderedlinkedlist.  *  *  @param  obj     item removed  */ public boolean remove(comparable obj) {     if(head == null) return false;     if(((comparable)(head.theitem)).compareto(obj) == 0) {     if(head == tail) {         head = tail = null;         return true;     }     head = head.next;     return true;     }      if(head == tail)return false;     orderedlistnode ref = head;     while( ref.next != tail) {         if(((comparable)(ref.next.theitem)).compareto(obj) == 0) {             ref.next = ref.next.next;             return true;         }         ref = ref.next;     }     if(((comparable)(tail.theitem)).compareto(obj) == 0 ) {         tail = ref;         tail.next = null;         return true;     }      return false;  }   /**  *  empty orderedlinkedlist.  */ public void clear() {     // reset header node     head = new orderedlistnode("head", null, null);      // reset tail node     tail = new orderedlistnode("tail", head, null);      // header references tail in empty linkedlist     head.next = tail;      // reset size 0     thesize = 0;      // emptying list counts modification     modcount++; }   /**  *  return true if orderedlinkedlist contains 0 items.  */ public boolean isempty() {     return thesize == 0; }   /**  *  return number of items in orderedlinkedlist.  */ public int size() {     return thesize; }   /*    *  return string representation of orderedlinkedlist.  *  *  (non-javadoc)  *  @see java.lang.object#tostring()  */ @override public string tostring() {     string s = "";      orderedlistnode currentnode = head.next;      while (currentnode != tail)     {         s += currentnode.theitem.tostring();          if (currentnode.next != tail)         {             s += ", ";         }          currentnode = currentnode.next;     }      return s; }  private static class orderedlistnode<comparable> {      comparable theitem;     orderedlistnode<comparable> next;     orderedlistnode<comparable> previous;      public orderedlistnode(comparable theitem, orderedlistnode<comparable> previous, orderedlistnode<comparable> next) {         this.theitem = theitem;         this.next = next;         this.previous = previous;     }  } 

}

this line wrong

if(((comparable)(head.theitem)).equals(obj)) { 

you should use comparable.compareto, besides should start head , go until find element greater or equal obj , insert obj before it, this

public boolean add(comparable obj) {     modcount++;     if (head == null) {         head = new orderedlistnode(obj, null, null);         return true;     }     (orderedlistnode current = head; current != null; current = current.next) {         if (((comparable) (current.theitem)).compareto(obj) >= 0) {             current.prev = new orderedlistnode(obj, current.prev, current);             return true;         }     }     tail.next = new orderedlistnode(obj, tail, null);     return true; } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -