用Java實現一個靜態連結串列的方法步驟
阿新 • • 發佈:2020-02-12
什麼是靜態連結串列?
對於線性連結串列,也可用一維陣列來進行描述。這種描述方法便於在沒有指標型別的高階程式設計語言中使用連結串列結構。
用陣列描述的連結串列,即稱為靜態連結串列。
在C語言中,靜態連結串列的表現形式即為結構體陣列,結構體變數包括資料域data和遊標CUR。
靜態連結串列的節點
資料域:用於儲存資料元素的值
遊標:即陣列下標,表示直接後繼元素所在陣列中的位置
public class StaticLinkedListNode<T> { public T data; // 資料 public int cursor; // 遊標 ... }
注:通常靜態連結串列會將第一個資料元素放到陣列下標為1(即a[1])的位置中。
備用連結串列
靜態連結串列中,除了資料本身通過遊標組成連結串列外,還需要有一條連線各個空閒位置的連結串列,稱為備用連結串列。
作用:回收陣列中未使用或者之前使用過(現在不用)的儲存空間,留待後期使用。即靜態連結串列使用陣列申請的物理空間中,存在兩個連結串列,一條連線資料,另一條連線陣列中為使用的空間。
注:通常備用連結串列的表頭位於陣列下標為0(a[0])的位置,而資料鏈表的表頭位於陣列下標為1(a[1])的位置。
靜態連結串列中設定備用連結串列的好處是,可以清楚地知道陣列中是否有空閒位置,以便資料鏈表新增新資料時使用。比如,若靜態連結串列中陣列下標為 0 的位置上存有資料,則證明陣列已滿。
完整程式碼
public class StaticLinkedListNode<T> { public T data; private int cursor; public StaticLinkedListNode(T data,int cursor) { this.cursor = cursor; } public T getData() { return data; } public void setData(T data) { this.data = data; } public int getCursor() { return cursor; } public void setCursor(int cursor) { this.cursor = cursor; } }
public class StaticLinkedList<T> { StaticLinkedListNode[] nodes; private static final int MAX_SIZE = 100; private int length; public StaticLinkedList() { nodes = new StaticLinkedListNode[MAX_SIZE]; for (int i = 0; i < MAX_SIZE; i++) { nodes[i] = new StaticLinkedListNode<T>(null,i + 1); } nodes[MAX_SIZE - 1].setCursor(0); this.length = 0; } // 連結串列實際長度 public int Length() { return length; } // 判斷使用陣列是否為空 public boolean isEmpty() { return length == 0; } // 判斷備用連結串列是否為空 public boolean isFull() { return length == MAX_SIZE; } // 插入一個節點 public boolean addTo(T data,int index) { if (isFull() || index > MAX_SIZE || index < -1 || data == null) return false; if (index == 0) { insert(data); return true; } if (index > Length()) { index = Length(); } // 獲取第一個使用節點的下標 int firstUse = nodes[MAX_SIZE - 1].getCursor(); // 獲取備用陣列第一個節點的下標 int firstNull = nodes[0].getCursor(); for (int i = 1; i < index; i++) { firstUse = nodes[firstUse].getCursor(); } // 獲取目標節點的後續節點 int nextUse = nodes[firstUse].getCursor(); int nextNull = nodes[firstNull].getCursor(); nodes[0].setCursor(nextNull); nodes[firstUse].setCursor(firstNull); nodes[firstNull].setCursor(nextUse); nodes[firstNull].setData(data); length++; return true; } public void insert(T data) { int t = nodes[MAX_SIZE - 1].getCursor(); int firstNull = nodes[0].getCursor(); nodes[MAX_SIZE - 1].setCursor(firstNull); nodes[0].setCursor(nodes[firstNull].getCursor()); nodes[firstNull].setCursor(t); nodes[firstNull].setData(data); length++; } public void print() { int first = nodes[MAX_SIZE - 1].getCursor(); System.out.println("連結串列:"); for (int i = first; i != 0; ) { System.out.print(nodes[i].getData() + " "); i = nodes[i].getCursor(); } } // 刪除指定節點 public boolean delete(T data) { if (isEmpty()) { return false; } int temp = MAX_SIZE - 1; while (temp != 0) { if (nodes[nodes[temp].getCursor()].getData() == data) { int p = nodes[temp].getCursor(); nodes[temp].setCursor(nodes[p].getCursor()); nodes[p].setCursor(nodes[0].getCursor()); nodes[0].setCursor(p); nodes[p].setData(null); length--; return true; } temp = nodes[temp].getCursor(); } return false; } // 刪除所有節點 public boolean deleteAll() { if (isEmpty()) { return true; } for (int i = 0; i < MAX_SIZE - 1; i++) { nodes[i].setCursor(i + 1); nodes[i].setData(null); } nodes[MAX_SIZE - 1].setCursor(0); nodes[MAX_SIZE - 1].setData(null); length = 0; return true; } public void printAll() { System.out.println("連結串列:"); for (int i = 0; i < MAX_SIZE; i++) { System.out.print("[" + i + " " + nodes[i].getData() + " " + nodes[i].getCursor() + "]"); if (i % 5 == 0 && i != 0) { System.out.println(); } } } public static void main(String[] args) { StaticLinkedList<String> list = new StaticLinkedList<String>(); list.insert("A"); list.insert("B"); list.insert("C"); list.insert("D"); list.insert("E"); list.addTo("X",2); System.out.println(list.Length()); list.print(); // list.printAll(); // list.deleteAll(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。