一、JAVA單向鏈表的操作(增加節點、查找節點、刪除節點)
class Link { // 鏈表類 class Node { // 保存每一個節點,此處為了方便直接定義成內部類 private String data; // 節點的內容 private Node next; // 保存下一個節點 public Node(String data) { // 通過構造方法設置節點內容 this.data = data; } public void add(Node node) { // 增加節點 if (this.next == null) { // 如果下一個節點為空,則把新節點加入到next的位置上 this.next = node; } else { // 如果下一個節點不為空,則繼續找next this.next.add(node); } } public void print() { // 打印節點 if (this.next != null) { System.out.print(this.data + "-->"); this.next.print(); } else { System.out.print(this.data + "\n"); } } public boolean search(String data) { // 內部搜索節點的方法 if (this.data.equals(data)) { return true; } if (this.next != null) { return this.next.search(data); } else { return false; } } public void delete(Node previous, String data) { // 內部刪除節點的方法 if (this.data.equals(data)) { previous.next = this.next; } else { if (this.next != null) { this.next.delete(this, data); } } } } private Node root; // 定義頭節點 public void addNode(String data) { // 根據內容添加節點 Node newNode = new Node(data); // 要插入的節點 if (this.root == null) { // 沒有頭節點,則要插入的節點為頭節點 this.root = newNode; } else { // 如果有頭節點,則調用節點類的方法自動增加 this.root.add(newNode); } } public void print() { // 展示列表的方法 if (root != null) { // 當鏈表存在節點的時候進行展示 this.root.print(); } } public boolean searchNode(String data) { // 在鏈表中尋找指定內容的節點 return root.search(data); // 調用內部搜索節點的方法 } public void deleteNode(String data) { // 在鏈表中刪除指定內容的節點 if (root.data.equals(data)) { // 如果是頭節點 if (root.next != null) { root = root.next; } else { root = null; } } else { root.next.delete(this.root, data); } } }
測試:
public class TestMain { public static void main(String[] args) { Link l = new Link(); l.addNode("A"); l.addNode("B"); l.addNode("C"); l.addNode("D"); System.out.println("原鏈表:"); l.print(); String searchNode = "B"; System.out.println("查找節點:" + searchNode); String result = l.searchNode(searchNode)?"找到!":"沒找到!"; System.out.println("查找結果:" + result); System.out.println("刪除節點:" + searchNode); l.deleteNode(searchNode); System.out.println("刪除節點后的鏈表:"); l.print(); } }
測試結果如下:
原鏈表: A-->B-->C-->D 查找節點:B 查找結果:找到! 刪除節點:B 刪除節點后的鏈表: A-->C-->D
二、雙向鏈表的簡單實現
public class DoubleLink<T> { /** * Node<AnyType>類定義了雙向鏈表中節點的結構,它是一個私有類, 而其屬性和構造函數都是公有的,這樣,其父類可以直接訪問其屬性 * 而外部類根本不知道Node類的存在。 * * @author ZHB * * @param <T> * 類型 * @param Data * 是節點中的數據 * @param pre * 指向前一個Node節點 * @param next * 指向后一個Node節點 */ private class Node<T> { public Node<T> pre; public Node<T> next; public T data; public Node(T data, Node<T> pre, Node<T> next) { this.data = data; this.pre = pre; this.next = next; } public Node() { this.data = null; this.pre = null; this.next = null; } } // 下面是DoubleLinkedList類的數據成員和方法 private int theSize; private Node<T> Header; private Node<T> Tail; /* * 構造函數 我們構造了一個帶有頭、尾節點的雙向鏈表 頭節點的Next指向尾節點 為節點的pre指向頭節點 鏈表長度起始為0。 */ public DoubleLink() { theSize = 0; Header = new Node<T>(null, null, null); Tail = new Node<T>(null, Header, null); Header.next = Tail; } public void add(T item) { Node<T> aNode = new Node<T>(item, null, null); Tail.pre.next = aNode; aNode.pre = Tail.pre; aNode.next = Tail; Tail.pre = aNode; theSize++; } public boolean isEmpty() { return (this.theSize == 0); } public int size() { return this.theSize; } public T getInt(int index) { if (index > this.theSize - 1 || index < 0) throw new IndexOutOfBoundsException(); Node<T> current = Header.next; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } public void print() { Node<T> current = Header.next; while (current.next != null) { System.out.println(current.data.toString()); current = current.next; } } public static void main(String[] args) { DoubleLink<String> dLink = new DoubleLink<String>(); dLink.add("zhb"); dLink.add("zzb"); dLink.add("zmy"); dLink.add("zzj"); System.out.println("size : " + dLink.size()); System.out.println("isEmpty? : " + dLink.isEmpty()); System.out.println("3 : " + dLink.getInt(2)); dLink.print(); } }