作者:伊丽汇 | 来源:互联网 | 2023-10-10 10:23
1packagecom.liu.Link;2测试类3classSortedListApp4{5publicstaticvoidmain(Stringargs[])6{7Sort
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 }
java实现有序链表,布布扣,bubuko.com