首页 > 其他 > 详细

有序链表

时间:2018-12-03 10:12:08      阅读:194      评论:0      收藏:0      [点我收藏+]

  其实就是重写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 }

 

有序链表

原文:https://www.cnblogs.com/AardWolf/p/10056395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!