資料結構 - 連結串列
連結串列
之前講到的動態陣列、棧、佇列,底層都是依託靜態陣列,靠resize解決固定容量問題。而連結串列是一種真正的動態線性的資料結構。
*最簡單的動態資料結構
*更加深入理解引用
*更深入的理解遞迴
*輔組組成其他資料結構
資料儲存在節點(Node)中,優點: 真正的動態,不需要處理固定容量問題,缺點:喪失了隨機訪問的能力。
陣列和連結串列的對比:
*陣列最要用於索引有語意的情況。
*最大的優點:支援快速查詢。
*連結串列不適合用於索引有語意的情況。
*最大的優點:動態。
實現連結串列的原始碼如下:
package linkedList;
/**
* 最簡單的動態資料結構連結串列的實現
*
* @author zhangtianci
*
*/
public class LinkedList<E> {
private class Node<E>{
E e;
Node next;
public Node(E e,Node<E> next){
this.e = e;
this.next = next;
}
public Node(E e){
this(e, null);
}
public Node(){
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private Node dummyHead; //設定一個虛擬頭結點
private int size; //記錄連結串列中元素的個數
/**
* 無參構造方法
*/
public LinkedList(){
dummyHead = new Node<>(null,null);
size = 0;
}
/**
* 獲得連結串列中元素的個數
*
* @return
*/
public int getSize(){
return size;
}
/**
* 判斷連結串列是否為空
*
* @return
*/
public boolean isEmpty(){
return size == 0;
}
/**
* 在連結串列的index位置上新增在,這在連結串列中並不是一個常用操作,練習用:)
*/
public void add(int index,E e){
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed.Illegal index.");
}
Node prev = dummyHead;
//獲取index位置的前一個元素
for(int i = 0; i < index; i++){
prev = prev.next;
}
prev.next = new Node<E>(e, prev.next);
size++;
}
/**
* 在連結串列頭新增一個新元素
*/
public void addFirst(E e){
add(0, e);
}
/**
* 在連結串列尾新增一個元素
* @param e
*/
public void addLast(E e){
add(size, e);
}
/**
* 獲取連結串列中索引為index的元素
*
* @param index
* @return
*/
public E get(int index){
if (index < 0 || index > size) {
throw new IllegalArgumentException("Get failed.Illegal index.");
}
Node<E> cur = dummyHead;
for(int i = 0; i < index; i++){
cur = dummyHead.next;
}
return cur.e;
}
/**
* 獲取連結串列的第一個元素
*
* @return
*/
public E getFirst(){
return get(0);
}
/**
* 獲取連結串列的最後一個元素
*
* @return
*/
public E getLast(){
return get(size - 1);
}
/**
* 修改連結串列中索引為index的值
*
* @param index
* @param e
*/
public void set(int index,E e){
if (index < 0 || index > size) {
throw new IllegalArgumentException("Get failed.Illegal index.");
}
Node<E> cur = dummyHead;
for(int i = 0; i < index; i++){
cur = dummyHead.next;
}
cur.e = e;
}
/**
* 檢視連結串列中是否存在該元素,若存在返回true,反之false
*
* @param e
* @return
*/
public boolean contains(E e){
Node cur = dummyHead.next;
while (cur != null) {
if (cur.e.equals(e)) {
return true;
}
cur = cur.next;
}
return false;
}
/**
* 移出連結串列中索引為index的元素並返回
*
* @param index
* @return
*/
public E remove(int index){
if (index < 0 || index > size) {
throw new IllegalArgumentException("Remove failed.Illegal index.");
}
Node prev = dummyHead;
for(int i = 0; i < index; i++){
prev = prev.next;
}
Node<E> cur = prev.next;
prev.next = cur.next;
cur.next = null;
size--;
return cur.e;
}
/**
* 移出連結串列中的第一個元素
*
* @return
*/
public E removeFirst(){
return remove(0);
}
/**
* 移出連結串列中的最後一個元素
*
* @return
*/
public E removeLast(){
return remove(size - 1);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
Node cur = dummyHead.next;
while(cur != null){
res.append(cur + "->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
}
簡單的時間複雜度分析:
*新增操作:
addLast(E e) : O(n)
addFirst(E e) : O(1)
add(int index,E e) : O(n)
刪除操作:
removeLast(E e) : O(n)
removeFirst(E e) : O(1)
remove(int index) : O(n)
修改操作:
set(int index,E e) : O(n)
查詢操作:
get(int index) : O(n)
contains(E e) : O(n)
綜上所述,最好不要做修改操作,查詢最好只要查連結串列頭元素(時間複雜度為O(1)),對於增加和刪除最好連結串列頭進行操作(時間複雜度為O(1)),同時連結串列是動態的不會大量的浪費空間,所以具有一定的優勢。對連結串列來說還具有很多改進的地方。