一、線性表簡介
線性表簡單地說就是資料元素的序列,即一對一關聯性;
二、ArrayList簡單實現
讀取:O(1)
插入、刪除:O(n)
代碼實現:
package org.xiazdong.list;public class MyArrayList<T> {private static final int DEFAULT_LENGTH = 10;private T[]t;private int length;public MyArrayList(){t = (T[])new Object[DEFAULT_LENGTH];length = 0;}public MyArrayList(int length){t = (T[])new Object[length];length = 0;}public MyArrayList(T[]arr){this(arr.length*2);for(int i=0;i<arr.length;i++){t[i] = arr[i];}}public boolean isEmpty(){return length == 0;}public void makeEmpty(){t = (T[])new Object[t.length];length = 0;}public T get(int i)throws ArrayIndexOutOfBoundsException{if(i<0||i>=length){throw new ArrayIndexOutOfBoundsException("數組越界");}else{return t[i];}}public boolean contains(T e){for(T elem:t){if(elem.equals(e)){return true;}}return false;}public boolean insert(T e){if(e==null){return false;}//如果容量不夠,則擴充if(length>=t.length){larger(length*2);}t[length++] = e;return true;}public boolean remove(T e){int pos = -1;for(int i=0;i<length;i++){if(t[i].equals(e)){pos = i;}}if(pos == -1){return false;}else{for(int j=pos;j<length-1;j++){t[j] = t[j+1];t[j+1] = null;}length--;return true;}}public boolean remove(int i){if(i<0||i>=length){throw new ArrayIndexOutOfBoundsException();}else{for(int j=i;j<length-1;j++){t[j]= t[j+1];t[j+1] = null;}length--;return true;}}public boolean insert(int i,T e){if(i<0||i>length){throw new ArrayIndexOutOfBoundsException();}if(length>=t.length){larger(length*2);}for(int j=t.length-1;j>=i;j--){t[j+1]= t[j];}t[i] = e;length++;return true;}private void larger(int len){T[]tmp = (T[])new Object[len];for(int i=0;i<t.length;i++){tmp[i] = t[i];}t = tmp;}public String toString(){StringBuilder buf = new StringBuilder();for(int i=0;i<length;i++){buf.append(t[i]).append(" ");}return buf.toString();}public int find(T e){for(int i=0;i<length;i++){if(t[i].equals(e)){return i;}}return -1;}public int getSize(){return length;}}
三、LinkedList簡單實現
讀取:O(n)
插入、刪除:O(1)
頭指標:第一個節點的儲存地址;
頭結點:在第一個資料節點前添加一個頭結點,可以儲存鏈表的長度,為了方便鏈表操作,因為比如:
代碼實現:
package org.xiazdong.list;/** * * * * @author xzdong * * @param <T> */public class MyLinkedList<T> {/** * first.elem儲存鏈表長度 */private BeginNode first;public MyLinkedList(){first = new BeginNode(0,null);}public void insert(T elem){Node n = new Node(elem,null);Node current = first;while(current.next!=null){current = current.next;}current.next = n;first.elem++;}public void insert(int i,T elem){if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}Node current = first;Node n = new Node(elem,null);int count = 0;while(current.next!=null&&count<i){current = current.next;count++;}n.next = current.next;current.next = n;first.elem++;}public boolean remove(int i,T elem){if(first.elem==0){return false;}if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}//1.找到位置Node current = first;int count = 0;while(current.next!=null&&count<i){current = current.next;count++;}//2.模版代碼current.next = current.next.next;first.elem--;return true;}public boolean isEmpty(){return first.elem==0;}public void makeEmpty(){first.next=null;}public int getSize(){return first.elem;}public int find(T elem){Node current = first;int index = 0;while(current.next!=null){current = current.next;if(current.elem.equals(elem)){return index;}else{index++;}}return -1;}public boolean remove(T elem){if(first.elem==0){return false;}Node current = first.next;Node beforeCurrent = first;while(!current.elem.equals(elem)){if(current.next==null){return false;}beforeCurrent = current;current = current.next;}if(current.elem.equals(elem)){beforeCurrent.next = current.next;first.elem--;return true;}else{return false;}}public T get(int i){if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}int count = 0;Node current = first;while(current.next!=null&&count<i){current = current.next;count++;}return current.next.elem;}public String toString(){StringBuilder buf = new StringBuilder();if(first.elem==0){return "";}Node current = first.next;while(current!=null){buf.append(current.elem).append(" ");current = current.next;}return buf.toString();}class Node{public Node(){elem = null;next = null;}public Node(T elem,Node next){this.elem = elem;this.next = next;}private T elem;Node next;}class BeginNode extends Node{int elem;public BeginNode(int elem,Node next){this.elem = elem;this.next = next;}}}
四、兩種實現的比較
| |
數組實現 |
鏈表實現 |
| 優點 |
讀取快 |
不需要預先分配、插入刪除方便 |
| 缺點 |
預先分配大小、插入刪除麻煩 |
讀取慢 |
迴圈鏈表實現迴圈鏈表就是尾指標指向頭指標,形成一個迴圈;代碼實現:
package org.xiazdong.list;/** * * * * @author xzdong * * @param <T> */public class MyCircularLinkedList<T> {/** * first.elem儲存鏈表長度 */private BeginNode first;public MyCircularLinkedList(){first = new BeginNode(0,null);first.next = first;}public void insert(T elem){Node n = new Node(elem,first);Node current = first;while(current.next!=first){current = current.next;}current.next = n;first.elem++;}public void insert(int i,T elem){if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}Node current = first;Node n = new Node(elem,null);int count = 0;while(current.next!=null&&count<i){current = current.next;count++;}n.next = current.next;current.next = n;first.elem++;}public boolean remove(int i,T elem){if(first.elem==0){return false;}if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}//1.找到位置Node current = first;int count = 0;while(current.next!=first&&count<i){current = current.next;count++;}//2.模版代碼current.next = current.next.next;first.elem--;return true;}public boolean isEmpty(){return first.elem==0;}public void makeEmpty(){first.next=first;}public int getSize(){return first.elem;}public int find(T elem){Node current = first;int index = 0;while(current.next!=first){current = current.next;if(current.elem.equals(elem)){return index;}else{index++;}}return -1;}public boolean remove(T elem){if(first.elem==0){return false;}Node current = first.next;Node beforeCurrent = first;while(!current.elem.equals(elem)){if(current.next==first){return false;}beforeCurrent = current;current = current.next;}if(current.elem.equals(elem)){beforeCurrent.next = current.next;first.elem--;return true;}else{return false;}}public T get(int i){if(i<0||i>first.elem){throw new ArrayIndexOutOfBoundsException();}int count = 0;Node current = first;while(current.next!=first&&count<i){current = current.next;count++;}return current.next.elem;}public String toString(){StringBuilder buf = new StringBuilder();if(first.elem==0){return "";}Node current = first.next;while(current!=first){buf.append(current.elem).append(" ");current = current.next;}return buf.toString();}class Node{public Node(){elem = null;next = null;}public Node(T elem,Node next){this.elem = elem;this.next = next;}private T elem;Node next;}class BeginNode extends Node{int elem;public BeginNode(int elem,Node next){this.elem = elem;this.next = next;}}}雙向鏈表介紹
在單鏈表的基礎上,每個節點增加pre指標,指向前驅;