有序链表

  其实就是重写insert()方法,添加时进行一段比较。因为是泛型,利用到Comparable类的compareTo()方法。

 1 public class SortedSinglyList<T extends Comparable<? super T>> extends SinglyList<T> {
 2     public SortedSinglyList() {
 3         super();
 4     }
 5     
 6     public Node<T> insert(T x){
 7         Node<T> p = head.next;
 8         Node<T> front = head;
 9         
10         while(p != null && x.compareTo(p.data) > 0){
11                 front = p;
12                 p = p.next;
13         }
14         front.next = new Node<T>(x,p);
15         len++;
16         return front.next;
17     }
18 }

猜你喜欢

转载自www.cnblogs.com/AardWolf/p/10056395.html