java - Implementing an ADT linked list from scratch -


i've got class project have build adt-based linked list scratch (meaning can't use standard java adts) , use sort bunch of state objects (that each contain linked list of cities) alphabetically. backbone of code is, obviously, handmade orderedlinkedlist class, , i'm having lot of trouble figuring out how implement, speficially, findoradd method iterates through list and, if argument passed not in list, adds in appropriate spot (and returns element if it's there). of things i've read implementing linked lists don't deal in adts , it's difficult convert in mind , still wrap head around it. (admittedly incomplete) oll code , accompanying iterator:

import java.util.iterator;  public class orderedlinkedlist<e extends comparable<e>> implements iterable<e>  {     private e first;     private e next;     private e last;     private e current;     private e temp;     private int size;      public orderedlinkedlist()      {         this.first = null;         this.next = null;         this.last = null;         this.current = null;         this.size = 0;     }      public e findoradd(e element)     {         e returnval = null;         iterator<e> listiter = this.iterator();          if (this.first == null)         {             this.first = element;             this.size++;         }          else              (int = 0; < this.size; i++)                 {                     if (listiter.next().compareto(element) == 1 && listiter.hasnext() == false)                     {                         temp = this.first;                         this.first = element;                         this.next = temp;                         this.size++;                     }                        else if (listiter.next().compareto(element) == 1 && listiter.hasnext() == true)                         continue;                     else if (listiter.next().compareto(element) == 0)                         returnval = element;                     else if (listiter.next().compareto(element) == -1)                     {                         temp = this.next;                         this.next = element;                                             }                                }          return returnval;     }      public iterator<e> iterator()     {         return new ordlistiterator<e>();     }      private class ordlistiterator<e> implements iterator<e>      {         private e nextnode;          public ordlistiterator()         {             //maybe needed here         }          public boolean hasnext()         {             return (next != null);         }          public e next()         {                    return (e) next;         }          public e first()         {             return (e) first;         }          public void remove()         {             //implement later         }     } } 

i've got compareto() methods in state , city classes override usual method still function same way. going wrong in findoradd? how going wrong? i'm not looking full correction of code or anything; i'm 99% sure under else block abysmal. need push in right direction: someplace foothold. i'd hugely appreciate advice.

i believe issue may lie in fact calling listiter.next() each condition may mean pushing iterator through @ each check. possibly should store @ beginning of loop use single object in comparisons...


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -