排序的单链表是指,各结点按data域的值递增或递减顺序链接。结点data域的对象必须是可比较大小的,即已经实现了java.lang.Comparable接口的compareTo()方法。此方法的API描述为:
compareTo
int compareTo(T o)
-
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)
The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
-
-
Parameters:
-
o
- the object to be compared.
Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Throws:
-
ClassCastException
- if the specified object's type prevents it from being compared to this object.
-
本例继承带头结点的单链表类HSLinkedList(点击打开链接),覆盖父类的add(element)方法,插入可比较大小的对象。
算法描述:从空单链表开始,逐个插入结点建立排序的单链表。每插入一个结点,首先要从单链表的第0个结点开始,将待插入元素element依次与当前结点的data值比较大小,以确定插入位置并进行插入操作。
程序如下:
package linearList;
public class SortedHSLinkedList<E> extends HSLinkedList<E>{
public SortedHSLinkedList(){
super();
}
/*
* 根据指定对象的大小,插入到指定的位置
*/
public boolean add(E element){
if(element==null||!(element instanceof Comparable)){
return false;//不能插入null或者非comparable对象
}
Comparable cmp = (Comparable)element;
Node<E> front = this.head;
Node<E> p = front.next;//front是p的前驱结点
while(p!=null&&cmp.compareTo(p.data)>0){//比较大小,如果要插入的元素大于p值
front = p;
p = p.next;//往后行动front和p指针一次
}
front.next = new Node<E>(element,p);//插入,把新插入的结点的引用赋值给front的域引用
if(p==null){
this.rear = front.next;//尾插入,修改尾指针
}
this.n++;
return true;
}
public static void main(String[] args) {
SortedHSLinkedList<Integer> list = new SortedHSLinkedList<Integer>();
int n = 10;
System.out.println("insert: ");
for(int i=0;i<n;i++){
int k = (int)(Math.random()*100);
list.add(new Integer(k));
System.out.print(k+" ");
}
System.out.println("\nlist:"+list.toString());
}
}
执行结果如下:
insert:
Exception in thread "main" java.lang.NullPointerException
at linearList.SortedHSLinkedList.add(SortedHSLinkedList.java:18)
at linearList.SortedHSLinkedList.main(SortedHSLinkedList.java:37)
对于这个结果我有点始料不及,根据报错的位置
Node<E> p = front.next;//front是p的前驱结点
再次分析了一下源代码,也没找到错误,,看来还得发到网上看看大家有什么想法?