有序链表
Posted aardwolf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有序链表相关的知识,希望对你有一定的参考价值。
其实就是重写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 }
以上是关于有序链表的主要内容,如果未能解决你的问题,请参考以下文章