Java 實現有序鏈表__Java

來源:互聯網
上載者:User

有序鏈表:

按關索引值排序。刪除鏈頭時,就刪除最小(/最大)的值,插入時,搜尋插入的位置。

插入時需要比較O(N),平均O(N/2),刪除最小(/最大)的在鏈頭的資料時效率為O(1),

如果一個應用需要頻繁的存取(插入/尋找/刪除)最小(/最大)的資料項目,那麼有序鏈表是一個不錯的選擇

優先順序隊列 可以使用有序鏈表來實現

有序鏈表的插入排序:

對一個無序數組,用有序鏈表來排序,比較的時間級還是O(N^2)

複製時間級為O(2*N),因為複製的次數較少,第一次放進鏈表資料移動N次,再從鏈表複製到數組,又是N次

每插入一個新的鏈結點,不需要複製移動資料,只需要改變一兩個鏈結點的鏈域

import java.util.Arrays;import java.util.Random;/** * 有序鏈表 對數組進行插入排序 * @author stone */public class LinkedListInsertSort<T extends Comparable<T>> {private Link<T> first;//首結點public LinkedListInsertSort() {}public boolean isEmpty() {return first == null;}public void sortList(T[] ary) {if (ary == null) {return;}//將數組元素插入進鏈表,以有序鏈表進行排序for (T data : ary) {insert(data);}//}public void insert(T data) {// 插入 到 鏈頭, 以從小到大排序Link<T> newLink = new Link<T>(data);Link<T> current = first, previous = null;while (current != null && data.compareTo(current.data) > 0) {previous = current;current = current.next;}if (previous == null) {first = newLink;} else {previous.next = newLink;}newLink.next = current;}public Link<T>  deleteFirst() {//刪除 鏈頭Link<T> temp = first;first = first.next; //變更首結點,為下一結點return temp;}public Link<T> find(T t) {Link<T> find = first;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 = first;first = first.next; //變更首結點,為下一結點return temp;}}Link<T> p = first;Link<T> q = first;while (!p.data.equals(t)) {if (p.next == null) {//表示到鏈尾還沒找到return null;} else {q = p;p = p.next;}}q.next = p.next;return p;}public void displayList() {//遍曆System.out.println("List (first-->last):");Link<T> current = first;while (current != null) {current.displayLink();current = current.next;}}public void displayListReverse() {//反序遍曆Link<T> p = first, q = first.next, t;while (q != null) {//指標反向,遍曆的資料順序向後t = q.next; //no3if (p == first) {// 當為原來的頭時,頭的.next應該置空p.next = null;}q.next = p;// no3 -> no1  pointer reversep = q; //start is reverseq = t; //no3 start}//上面迴圈中的if裡,把first.next 置空了, 而當q為null不執行迴圈時,p就為原來的最且一個資料項目,反轉後把p賦給firstfirst = p; displayList();}class Link<T> {//鏈結點T data;//資料域Link<T> next; //後繼指標,結點鏈域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("----排序前----");System.out.println(Arrays.toString(ary));System.out.println("----鏈表排序後----");list.sortList(ary);list.displayList();}}
列印

----排序前----[595, 725, 310, 702, 444]----鏈表排序後----List (first-->last):the data is 310the data is 444the data is 595the data is 702the data is 725


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.