# 链表结构之有序链表

package
com.dbstructor.oop5;

class

public

int
dData;

public

public
int
dd){
dData

=
dd;
}

public

void
System.out.print(dData

+

);
}
}

class
SortedList {

public

public
first

=

null
;

for
(
int
i
=

0
; i
<
++
){
}
}

public

void
insertFirst(
int
dd){

=

new

=

null

//
这个标志很重要用来判定是否是表头

=
first;

//
如果current在表头或者表尾并且找到一个大于比较数据的链结点就退出循环

while
(current
!=

null

&&
current.dData
<
dd ){
privious

=
current;
current

=
current.next;
}

//
如果在表头就让首链结点直接指向

if
(privious
==

null
) {
first

=
}

//
否则在表尾让最后一个结点指向他

else
{
privious.next

=
}

//
不管如何这个结点的next指向最后

=
current;
}

public

=
first;
first

=
first.next;

return
first;
}

public

void
displayList() {

=
first;

while
(current
!=

null
){
current

=
current.next;
}
System.out.println();
}

}

public

class
SortedListApp {

public

static

void
main(String[] args) {

int
size
=

10
;

=

new
10
];

for
(
int
i
=

0
; i
<
++
){

=

new
int
)(Math.random()
*

99
));

=
}

System.out.print(

unSorted:

);

for
(
int
i
=

0
; i
<
++
){

+

);
}
System.out.println();
System.out.print(

Sorted:

);
SortedList theList

=

new
theList.displayList();
}

}

unSorted:

36

40

6

19

12

31

27

40

56

14

Sorted:

6

12

14

19

27

31

36

40

40

56

原文作者：算法小白
原文地址: https://www.cnblogs.com/bluedream2009/archive/2009/09/03/1559677.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。