Skip to content

Instantly share code, notes, and snippets.

@m0ar
Created February 18, 2015 12:39
Show Gist options
  • Save m0ar/0a9b6f1c803739d92c73 to your computer and use it in GitHub Desktop.
Save m0ar/0a9b6f1c803739d92c73 to your computer and use it in GitHub Desktop.
public boolean add(E element) {
if (element == null)
throw new NullPointerException("Can't insert a null element");
else if(head == null) {
head = new Entry(element, null);
return true;
}
Entry cmp = head;
//Step until the list ends or we're at our proper position
while(cmp.next != null && element.compareTo(cmp.next.element) > 0){
cmp = cmp.next;
}
cmp.next = new Entry(element, cmp.next);
return true;
}
public E get(E element) {
Entry walk = head;
//Step further while the list hasn't ended and until we have a match
while (walk != null && walk.element.compareTo(element) != 0)
walk = walk.next;
//Return null if the list ended, otherwise the element
return walk == null ? null : walk.element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment