从零开始学数据结构和算法(二)线性表的链式存储结构
生活随笔
收集整理的這篇文章主要介紹了
从零开始学数据结构和算法(二)线性表的链式存储结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈表
-
鏈式存儲結構
-
定義
- 線性表的鏈式存儲結構的特點是用一組任意的存儲單元的存儲線性表的數據元素,這組存儲單元是可以連續的,也可以是不連續的。
-
種類
- 結構圖
- 單鏈表
- 應用:MessageQueue
- 插入 enqueueMessage(Message msg,Long when)。
- 刪除 next ()。
- 單循環鏈表
- 雙鏈表
- LinkedList
- 雙向循環鏈表
-
優缺點
-
優點:插入刪除快
-
缺點:不支持隨即訪問
-
學習例子
基于系統 API LinkedList 將麻將進行分組排序
-
思想
-
邏輯步驟
- 把所有的點數分別裝入到對應的鏈表組中
- 在把鏈表中所有的點數合并在一起
- 在把所有的的點數分別裝在對應的鏈表中
- 最后把對應的點數鏈表裝在一個新的集合中
-
代碼編寫
/*** 麻將數據 Bean*/ public class Mahjong {public int suit;//筒,萬,索public int rank;//點數 一 二 三public Mahjong(int suit, int rank) {this.suit = suit;this.rank = rank;}public String toString() {return "("+this.suit+" "+this.rank+")";} } 復制代碼/*** 用系統自帶的 鏈表結構 LinkedList 來進行將麻將排序* @param list*/ private void radixSort(LinkedList<Mahjong> list) {//1. 先把所有的點數分別裝入到對應的鏈表組中//創建一個點數最大的為 9 的集合LinkedList[] linkedList = new LinkedList[9];//先初始化這 9 個鏈表目的是裝所有的對應的點數for (int i = 0; i < 9; i++) {linkedList[i] = new LinkedList();}while (list.size() > 0) {//取出對應的元素放入到鏈表中Mahjong mahjong = list.remove();//mahjong.rank - 1 的意思就是對應的集合下標linkedList[mahjong.rank - 1].add(mahjong);}//2. 然后再把所有的鏈表中的點數合并在一起for (int i = 0; i < linkedList.length; i++) {list.addAll(linkedList[i]);}//3. 在把所有的點數分別裝入到對應的鏈表中//花色有 3 個,那么我們就創建 3 個鏈表來代表裝入對應的花色LinkedList[] linkedLists =new LinkedList[3];for (int i = 0; i < 3; i++) {linkedLists[i] = new LinkedList();}//把對應的花色裝在對應的鏈表中while (list.size()>0){Mahjong mahjong = list.remove();linkedLists[mahjong.suit - 1].add(mahjong);}//4. 最后在把對應的點數鏈表裝在一個新的集合中for (int i = 0; i < linkedLists.length; i++) {list.addAll(linkedLists[i]);} } 復制代碼 -
應用
數據量幾十個,插入操作多的情況
編寫雙向鏈表 LinkedList CURD
-
參考圖解
-
編寫代碼
-
測試代碼
@Test public void testCustomLinkedList() {CustomLinkedList<Integer> linkedList = new CustomLinkedList<Integer>();linkedList.add(22);linkedList.add(2);linkedList.add(77);linkedList.add(6);linkedList.add(43);linkedList.add(76);linkedList.add(89);linkedList.add(0,0);for (int i = 0; i < linkedList.size; i++) {int integer = linkedList.get(i);System.out.println("--CustomLinkedList--CustomLinkedList" +integer+"");}System.out.println("\n\n");Integer remove = linkedList.remove();System.out.println("--CustomLinkedList--CustomLinkedList" +remove);Integer remove1 = linkedList.remove();System.out.println("--CustomLinkedList--CustomLinkedList" +remove1+"");Integer remove2 = linkedList.remove();System.out.println("--CustomLinkedList--CustomLinkedList" + remove2 + "");System.out.println("\n\n");for (int i = 0; i < linkedList.size; i++) {int integer = linkedList.get(i);System.out.println("--CustomLinkedList--CustomLinkedList" +integer+"");} } 復制代碼 -
測試結果
編寫簡單的 ArrayList CURD
public class CustomArrayList<E> {/*** 默認的空元素對象*/private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};/*** 空元素數據*/private static final Object[] EMPTY_ELEMENTDATA = {};/*** 默認的元素對象*/private Object[] elementData = null;/*** 容量大小*/private int size = 0;/*** 默認的容量大小*/private static final int DEFAULT_CAPACITY = 10;/*** 最大的數量** TODO------------減 8 是什么意思沒有搞懂*/private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;/*** 賦值為一個空對象*/public CustomArrayList(){elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}/*** 外部指定初始化一個容量大小* @param initCapacity*/public CustomArrayList(int initCapacity){if (initCapacity > 0){elementData = new Object[initCapacity];}else if (initCapacity == 0){elementData = EMPTY_ELEMENTDATA;}else {throw new IllegalArgumentException("Illegal Capacity: "+initCapacity);}}/*** 添加數據*/public boolean add(E e){//判斷是否需要開辟容量空間checkIsNeedCapacity(size + 1);//添加添加數據elementData[size++] = e;return true;}private void checkIsNeedCapacity(int minCapacity) {if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA){minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);}ensureExplicitCapacity(minCapacity);}private void ensureExplicitCapacity(int minCapacity) {// overflow-conscious codeif (minCapacity - elementData.length > 0)grow(minCapacity);}/*** 開辟空間的核心代碼* @param minCapacity*/private void grow(int minCapacity) {// overflow-conscious codeint oldCapacity = elementData.length;int newCapacity = oldCapacity + (oldCapacity >> 1);if (newCapacity - minCapacity < 0)newCapacity = minCapacity;if (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);// minCapacity is usually close to size, so this is a win:elementData = Arrays.copyOf(elementData, newCapacity);}private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflowthrow new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;} } 復制代碼編寫單向鏈表結構的 CURD
/*** Created by yangk on 2019/2/19.*/public class SingleLinked<T> {/*** 頭節點*/Node<T> head;/*** 鏈表長度*/int size = 0;/*** 將數據添加到鏈表中** @param data*/public void add(T data) {Node node = new Node(data);if (head == null) {head = node;return;}Node<T> tmp = head;while (tmp.next != null) {tmp = tmp.next;}tmp.next = node;size++;}/*** 將數據添加到第一個位置** @param data*/public void addHead(T data) {Node node = new Node(data);if (head == null) {head = node;size++;return;}Node n = head;node.next = n;head = node;size++;}public void add(int index, T data) {Node node = new Node(data);if (index == 0) {addHead(data);} else {Node tmp = head;for (int i = 0; i < index - 1; i++) {tmp = tmp.next;}Node n = tmp.next;tmp.next = node;node.next = n;size++;}}/*** 全部清除數據*/public void clear() {head = null;size = 0;}public boolean remove(int index) {//2Node<T> tmp = head;if (index == 0) {Node<T> newHead = tmp.next;head = null;head = newHead;size--;return true;}if (index < size) { // 1 2 3 4 5 6 7 3for (int i = 0; i < index - 2; i++) {tmp = tmp.next;}Node<T> pre = tmp;//要刪除的節點Node<T> next = tmp.next;Node<T> p = tmp.next.next;pre.next = p;next = null;size--;return true;}return false;}/*** 節點數據*/private class Node<T> {T data;Node<T> next;public Node(T data, Node<T> next) {this.data = data;this.next = next;}public Node(T next) {this.data = next;}}public void println() {if (head == null) return;Node n = head;while (n != null){System.out.println(n.data + "\n");n = n.next;}} } 復制代碼數據結構系列文章
- 從零開始學數據結構和算法(一)冒泡與選擇排序
- 從零開始學數據結構和算法(二)線性表的鏈式存儲結構
轉載于:https://juejin.im/post/5c9449dd5188252da22508e3
總結
以上是生活随笔為你收集整理的从零开始学数据结构和算法(二)线性表的链式存储结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [译]Web 性能优化: 图片优化让网站
- 下一篇: REdis AOF文件结构分析