break;
current = next;
next = (SkipListNode)current.nextNodes.get(level);
}
return current;
}
public int size() {
return size;
}
public boolean contains(Object o) {
E e = (E)o;
SkipListNode node = find(e);
return node != null &&
node.getValue() != null &&
equalTo((E)node.getValue(),e);
}
public Iterator iterator() {
return new SkipListIterator(this);
}
/******************************************************************************
* Utility Functions *
******************************************************************************/
private boolean lessThan(E a, E b) {
return a.compareTo(b) <0;
}
private boolean equalTo(E a, E b) {
return a.compareTo(b) &#61;&#61; 0;
}
private boolean greaterThan(E a, E b) {
return a.compareTo(b) > 0;
}
/******************************************************************************
* Testing *
******************************************************************************/
public static void main(String[] args) {
SkipList testList &#61; new SkipList();
System.out.println(testList);
testList.add(4);
System.out.println(testList);
testList.add(1);
System.out.println(testList);
testList.add(2);
System.out.println(testList);
testList &#61; new SkipList();
System.out.println(testList);
testList.add("hello");
System.out.println(testList);
testList.add("beautiful");
System.out.println(testList);
testList.add("world");
System.out.println(testList);
}
public String toString() {
String s &#61; "SkipList: ";
for(Object o : this)
s &#43;&#61; o &#43; ", ";
return s.substring(0,s.length()-2);
}
}
SkipListIterator.java
package skiplist_m;
/******************************************************************************
* SkipListIterator *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/
import java.util.*;
public class SkipListIterator> implements Iterator {
SkipList list;
SkipListNode current;
public SkipListIterator(SkipList list) {
this.list &#61; list;
this.current &#61; list.getHead();
}
public boolean hasNext() {
return current.nextNodes.get(0) !&#61; null;
}
public E next() {
current &#61; (SkipListNode)current.nextNodes.get(0);
return (E)current.getValue();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
SkipListNode.java
package skiplist_m;
/******************************************************************************
* SkipListNode *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/
import java.util.*;
public class SkipListNode {
private E value;
public List > nextNodes;
public E getValue() {
return value;
}
public SkipListNode(E value) {
this.value &#61; value;
nextNodes &#61; new ArrayList >();
}
public int level() {
return nextNodes.size()-1;
}
public String toString() {
return "SLN: " &#43; value;
}
}