Ordered linked list:
Sorted by key value. When you delete the chain header, delete the minimum (/maximum) value, and when you insert it, search for the insertion position.
Inserts need to compare O (N), mean O (n/2), delete minimum (/maximum) of data in the chain header when the efficiency is O (1),
If an application requires frequent access (insert/Find/delete) of the smallest (/largest) data item, then the ordered list is a good choice
Priority queues can be implemented using ordered linked lists
Insert ordering of ordered linked lists:
For an unordered array, sorted with an ordered list, the time level of comparison or O (n^2)
The replication time level is O (2*n), because the number of copies is less, the first place in the list of data to move n times, and then copied from the list to the array, but also N times
Each insertion of a new chain node, do not need to replicate the mobile data, only need to change one or two chain nodes of the chain field
Import Java.util.Arrays;
Import Java.util.Random; /** * Sequential list array Insert sort * @author stone/public class Linkedlistinsertsort<t extends comparable<t>> {pri Vate link<t>;
The first node public linkedlistinsertsort () {} is public Boolean IsEmpty () {return a null;
public void Sortlist (t[] ary) {if (ary = = null) {return;
////insert array elements into the list, sort the ordered list for (T data:ary) {insert (data);
}//} public void Insert (T data) {//inserted into the chain header to sort link<t> NewLink = new link<t> (data) from small to large;
Link<t> current = i, previous = null;
While (the current!= null && data.compareto (Current.data) > 0) {previous = current;
current = Current.next;
} if (previous = = null) {a = NewLink;
else {previous.next = NewLink;
} Newlink.next = current;
Public link<t> Deletefirst () {//delete chain head link<t> temp = first; i = First.next;
Change the first node to return temp for the next node; } public LINK<
T> Find (T-t) {link<t> find = i;
While [find!= null] {if (!find.data.equals (t)) {find = Find.next;
} else {break;
} return find;
Public link<t> Delete (T-t) {if (IsEmpty ()) {return null;
else {if (first.data.equals (t)) {link<t> temp = i; i = First.next;
Change the first node to return temp for the next node;
} link<t> p = i;
link<t> q = A;
while (!p.data.equals (t)) {if (P.next = = null) {//indicates that return null was not found at the end of the chain;
else {q = p;
p = p.next;
} q.next = P.next;
return p;
public void Displaylist () {//Traversal System.out.println ("List (first-->last):");
link<t> current = i;
while (current!= null) {Current.displaylink ();
current = Current.next;
The public void Displaylistreverse () {//Reverse sequence traversal link<t> p = A, q = First.next, T; while (q!= null) {//pointer reverse, traversal data order backwards t = Q.next;//NO3 if (p = = first) {//When the head is in the original header.Next should empty p.next = null; } Q.next = p;//no3-> no1 pointer p = q; The start is reverse q = t;
NO3 start}//In the IF in the loop above, the First.next is empty, and when q is null and does not perform a loop, p is the oldest and one data item, and then the P is assigned to the initial, p;
Displaylist (); Class Link<t> {//Chain node T data; data field link<t> next;
Subsequent pointers, node link domain link (T data) {this.data = data;
} void DisplayLink () {System.out.println ("The data is" + data.tostring ()); } public static void Main (string[] args) {linkedlistinsertsort<integer> list = new linkedlistinsertsort<
Integer> ();
Random Random = new Random ();
int len = 5;
integer[] ary = new Integer[len];
for (int i = 0; i < len; i++) {Ary[i] = random.nextint (1000);
} System.out.println ("before--------sort");
System.out.println (arrays.tostring (ary));
SYSTEM.OUT.PRINTLN ("----after sorting----linked list");
List.sortlist (ary);
List.displaylist ();
}
}Print
----sort before----
[595, 725, 310, 702, 444]
----sorted list----
list (first-->last): The
data is 310
the The data is 444 the ' data is ' 595 the ' data is 702 ' The data is
725