原始碼分析之手寫ExtLinkedList
阿新 • • 發佈:2018-12-06
public class ExtLinkedList<E> { // 連結串列實際儲存元素 private int size; // 第一個元素(頭節點,為了查詢) private Node first; // 最後一個元素(尾節點,為了新增) private Node last; // add public void add(E e) { Node node = new Node();// node.object = e; if (first == null) { // 新增第一個元素 // 給第一個元素賦值給node節點 first = node; // 以一個元素的首尾都是其本身 } else { // 新增第二個或以上資料 node.prev = last; last.next = node;// 上一個元素的next賦值 } last = node; size++; } // 下標新增 public void add(int index, E e) { // 下標的驗證 checkElementIndex(index); /*if (index == size) {// add(e); }*/ Node oldNode = getNode(index);// 原本下標index的節點 node2 if (oldNode != null) { //Node oldNextNode = oldNode.next;// nodel3 Node oldPrevNode = oldNode.prev;// node1 Node newNode = new Node();// node4 newNode.object = e; // node4的下一個節點 newNode.next = oldNode; if (oldPrevNode == null) { first = newNode; } else { // node4的上一個節點 newNode.prev = oldPrevNode; // node1的下個節點 oldPrevNode.next = newNode; } // node2的下個節點 oldNode.prev = newNode; } size++; } // get public Object get(int index) throws Exception { if (!isElementIndex(index)) throw new IndexOutOfBoundsException("越界了"); Node node = null; if (first != null) { node = first; for (int i = 0; i < index; i++) { node = node.next; } } return node.object; } public void remove(int index) throws Exception { if (!isElementIndex(index)) throw new IndexOutOfBoundsException("越界了"); ExtLinkedList.Node oldNode = getNode(index); if (oldNode != null) { // 獲取刪除節點的上下節點 // node3 ExtLinkedList.Node nextNode = oldNode.next; // node1 ExtLinkedList.Node prevNode = oldNode.prev; if (prevNode != null) { prevNode.next = nextNode; oldNode.prev = null; } else { first = nextNode; } if (nextNode != null) { nextNode.prev = prevNode; oldNode.next = null; } else { last = prevNode; } nextNode.object = null; size--; } /* * oldNode.prev.next = oldNode.next; oldNode.next.prev = oldNode.prev; */ } // 獲取node public Node getNode(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException("越界了"); Node node = null; if (first != null) { node = first; for (int i = 0; i < index; i++) { node = node.next; } } return node; } public int getSize() { return size; } // 連結串列節點儲存元素 private static class Node { Object object;// 當前元素 Node prev;// 上一個元素 Node next;// 寫一個元素 } private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException("越界"); } private boolean isElementIndex(int index) { return index >= 0 && index < size; } private String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + size; } public static void main(String[] args) throws Exception { ExtLinkedList extLinkedList = new ExtLinkedList(); extLinkedList.add("a"); extLinkedList.add("b"); extLinkedList.add("c"); extLinkedList.add("e"); System.out.println("前"+extLinkedList.size); System.out.println(extLinkedList.get(3)); extLinkedList.add(3, "d"); System.out.println("後"+extLinkedList.size); System.out.println(extLinkedList.get(3)); /*for (int i = 0; i < extLinkedList.size-1; i++) { Object object = extLinkedList.get(i); System.out.println(object); }*/ /* * for (int i = 0; i < extLinkedList.size; i++) { * System.out.println("--" + extLinkedList.get(i)); } */ } }