Ordered linked list:
Sort by key value.
When you delete a chain header, the minimum (/maximum) value is removed, and when you insert it, search for the inserted location.
An O (N), an average O (N/2), and a minimum (/maximum) of the data at the chain head are required to be inserted at O (1),
If an application requires frequent access (insert/Find/delete) of minimum (/maximum) data items, then an ordered list is a good choice
The priority queue is able to use an ordered list to implement
Sequential list Insertion Sort:
An unordered array, sorted by an ordered list, the time level or O (n^2)
Replication Time level O (2*n), due to fewer copies, the first time to put in the linked list of data moved n times, and then copied from the linked list to the array, n times
Each new link node is inserted without the need to copy the mobile data. Only need to change the chain domain of one or two link nodes
Import Java.util.arrays;import java.util.random;/** * ordered list of arrays for insertion sort * @author stone */public class linkedlistinsertsort& Lt T extends comparable<t>> {private link<t> first;//first node public linkedlistinsertsort () {}public Boolean IsEmpty () {return first = = null;} public void Sortlist (t[] ary) {if (ary = = null) {return;} Inserts an array element into the list and sorts the ordered list for (T data:ary) {insert (data);} }public void Insert (T data) {//inserted into the chain header, sorted from small to large link<t> NewLink = new link<t> (data); Link<t> current = First, previous = Null;while (current! = null && data.compareto (Current.data) > 0) {Pre vious = Current;current = Current.next;} if (previous = = null) {first = NewLink;} else {previous.next = NewLink;} Newlink.next = current;} Public link<t> Deletefirst () {//delete chain header link<t> temp = First;first = First.next;//change First node, return temp for next node;} Public link<t> Find (T-t) {link<t> find = First;while (find! = null) {if (!find.data.equals (t)) {find = Find.ne XT;} else {break;}} REturn find; }public link<t> Delete (T-t) {if (IsEmpty ()) {return null;} else {if (first.data.equals (t)) {link<t> temp = Fir St;first = First.next; Change the first node, return temp for the next node;}} Link<t> p = First; link<t> q = first;while (!p.data.equals (T)) {if (P.next = = null) {//indicates that no return null was found at the end of the chain;} else {q = P;p = P.next;} }q.next = P.next;return p;} public void Displaylist () {//Traverse System.out.println ("List (first-->last):"); Link<t> current = First;while (current! = null) {Current.displaylink (); current = Current.next;}} public void Displaylistreverse () {//reverse-order traversal link<t> p = First, q = First.next, t;while (q! = null) {//pointer reverse, traverse data order Backward T = Q.next; No3if (p = = first) {//when the original header, the. Next of the header should be empty p.next = null;} Q.next = p;//NO3, no1 pointer reversep = q; Start is reverseq = t; NO3 start}//The IF in the above loop. The First.next is empty, and when q is null does not run the loop, p is the original most and one data item, after reversal P is assigned to Firstfirst = p; Displaylist ();} Class Link<t> {//Link node T data;//data field link<t> next;//successor pointer, 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& gt; (); Random random = new random (), int len = 5;integer[] ary = new Integer[len];for (int i = 0; i < len; i++) {Ary[i] = Rando M.nextint (1000);} SYSTEM.OUT.PRINTLN ("--------before sorting"); System.out.println (arrays.tostring (ary)); SYSTEM.OUT.PRINTLN ("----list----after sorting"); list.sortlist (ary); List.displaylist ();}}Print
----sort before----[595, 725, 310, 702, 444]----list after sorting----lists (First-->last): The data is 310the data is 444the data is 595the D ATA is 702the data is 725
Implementing an ordered list in Java