首页 > 编程语言 > 详细

java实现有序链表

时间:2014-04-22 03:19:27      阅读:506      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 package com.liu.Link;
 2 //测试类
 3 class SortedListApp
 4 {
 5     public static void main(String args[])
 6     {
 7         SortedList theSortedList = new SortedList();
 8         theSortedList.insert(40);
 9         theSortedList.insert(20);
10         theSortedList.displayList();
11         
12         theSortedList.insert(10);
13         theSortedList.insert(15);
14         theSortedList.insert(50);
15         theSortedList.displayList();
16         
17         theSortedList.remove();
18         theSortedList.displayList();
19         
20     }
21 }
22 
23 
24 
25 
26 
27 //有序链表实现类
28 public class SortedList {
29     private Link5 first;
30     public SortedList(){
31         first = null;
32     }
33     public boolean isEmpty()
34     {
35         return first==null;
36     }
37     public void insert(long d){
38         Link5 newLink = new Link5(d);
39         Link5 previous = null;
40         Link5 current = first;
41         
42         while(current!=null&&d>current.dData){
43             previous = current;
44             current = current.next;
45         }
46         //当有序链表是一个空表时
47         if(previous == null)
48         {
49             first = newLink;
50         }else
51         {
52             previous.next = newLink;
53         }
54         newLink.next = current;
55     }
56     
57     public Link5 remove()
58     {
59         Link5 temp = first;
60         first = first.next;
61         return temp;
62     }
63     
64     public void displayList()
65     {
66         System.out.print("List (first-->last):");
67         Link5 current = first;
68         while(current!=null)
69         {
70             current.displayLink();
71             current = current.next;
72         }
73         System.out.println("");
74     }
75 }
76 //有序链表数据类
77 class Link5
78 {
79     public long dData;
80     public Link5 next;
81     public Link5(long d){
82         dData = d;
83     }
84     public void displayLink(){
85         System.out.print(dData+" ");
86     }
87 }
bubuko.com,布布扣

 

java实现有序链表,布布扣,bubuko.com

java实现有序链表

原文:http://www.cnblogs.com/speaklessdomore/p/3678189.html

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