首頁 Java java教程 Java模擬有序鍊錶資料結構的範例

Java模擬有序鍊錶資料結構的範例

Jan 24, 2017 pm 04:02 PM

有序鍊錶:
依關鍵值排序。刪除鏈頭時,就刪除最小(/最大)的值,插入時,搜尋插入的位置。
插入時需要比較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; //no3 
      if (p == first) {// 当为原来的头时,头的.next应该置空 
        p.next = null; 
      } 
      q.next = p;// no3 -> no1 pointer reverse 
      p = q; //start is reverse 
      q = t; //no3 start 
    } 
    //上面循环中的if里,把first.next 置空了, 而当q为null不执行循环时,p就为原来的最且一个数据项,反转后把p赋给first 
    first = 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 310
the data is 444
the data is 595
the data is 702
the data is 725
登入後複製

   


序:

public class SingleLinkedListReverse {
    
  public static void main(String[] args) {
    Node head = new Node(0);
    Node temp = null;
    Node cur = null;
      
    for (int i = 1; i <= 10; i++) {
      temp = new Node(i);
      if (i == 1) {
        head.setNext(temp);
      } else {
        cur.setNext(temp);
      }
      cur = temp;
    }//10.next = null;
      
    Node h = head;
    while (h != null) {
      System.out.print(h.getData() + "\t");
      h = h.getNext();
    }
    System.out.println();
      
    //反转1
//   h = Node.reverse1(head);
//   while (h != null) {
//     System.out.print(h.getData() + "\t");
//     h = h.getNext();
//   }
      
    //反转2
    h = Node.reverse1(head);
    while (h != null) {
      System.out.print(h.getData() + "\t");
      h = h.getNext();
    }
      
      
  }
}
  
/*
 * 单链表的每个节点都含有指向下一个节点属性
 */
class Node {
  Object data;//数据对象 
  Node next; //下一节点
    
  Node(Object d) {
    this.data = d;
  }
  Node(Object d, Node n) {
    this.data = d;
    this.next = n;
  }
  public Object getData() {
    return data;
  }
  public void setData(Object data) {
    this.data = data;
  }
  public Node getNext() {
    return next;
  }
  public void setNext(Node next) {
    this.next = next;
  }
  //方法1 head被重置
  static Node reverse1(Node head) {
  
    Node p = null; //反转后新的 头
    Node q = head;
    //轮换结果:012,123,234,.... 10 null null
    while (head.next != null) {
      p = head.next;   // 第1个 换成第2个 这时p表示原始序列头中的next
      head.next = p.next; // 第2个 换成第3个
      p.next = q;     //已经跑到第1位置的原第2个的下一个 就要变成 原第1个
      q = p;       //新的第1个 要变成 当前第一个
    }
    return p;
      
  }
  //方法2 head没重置
  static Node reverse2(Node head) {
    //将中间节点的指针指向前一个节点之后仍然可以继续向后遍历链表
    Node p1 = head, p2 = head.next, p3; // 前 中 后
    //轮换结果 :012, 123, 234, 345, 456.... 9 10 null
    while (p2 != null) {
      p3 = p2.next; 
      p2.next = p1; //指向后 变 指向前
      p1 = p2;   //2、3向前挪
      p2 = p3;
    }
    head.next = null;//head没变,当输出到0时,再请求0.next 为1
    return p1;
  }
}
登入後複製
   


更多Java模擬有序鍊錶資料結構的範例相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致應用無法運行?如何排查和解決? 公司安全軟件導致應用無法運行?如何排查和解決? Apr 19, 2025 pm 04:51 PM

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

如何使用MapStruct簡化系統對接中的字段映射問題? 如何使用MapStruct簡化系統對接中的字段映射問題? Apr 19, 2025 pm 06:21 PM

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

如何優雅地獲取實體類變量名構建數據庫查詢條件? 如何優雅地獲取實體類變量名構建數據庫查詢條件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

如何將姓名轉換為數字以實現排序並保持群組中的一致性? 如何將姓名轉換為數字以實現排序並保持群組中的一致性? Apr 19, 2025 pm 11:30 PM

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對像如何安全地轉換為數組? Java對像如何安全地轉換為數組? Apr 19, 2025 pm 11:33 PM

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? 電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? Apr 19, 2025 pm 11:27 PM

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名構建查詢條件? 使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名構建查詢條件? Apr 19, 2025 pm 09:51 PM

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...

See all articles