1.單鏈表的實現:
package com.sun;public class DanLink { public static void main(String[] args) { DanLink link = new DanLink(); link.addFirstNode("sunchaung"); link.add(1, "張樂"); link.addTailNode("習大大"); link.addFirstNode("maozed"); // link.deleteByData("習大大"); // link.deleteByPos(2); Node findByData = link.findByData("習大大"); findByData.display(); Node findByPos = link.findByPos(3); findByPos.display(); link.displayAllNodes(); } PRivate Node first; // 定義一個頭結點 private int pos = 0;// 節點的位置 private int size; public DanLink() { this.first = null; this.size = 0; } // 插入一個頭節點 public void addFirstNode(Object data) { Node node = new Node(data); node.next = first; first = node; size++; } // 插入一個尾節點 public void addTailNode(Object data) { if (size == 0 || first == null) { return; } Node node = first; while (node.next != null) { node = node.next; if (node.next == null) { break; } } // 找到了最后一個節點 Node node2 = new Node(data); node.next = node2; node2.next = null; size++; } // 刪除一個頭結點,并返回頭結點 public Node deleteFirstNode() { if (null == first || size == 0) { return null; } if (size == 1) { Node node = first; first.data = null; first.next = null; size--; return node; } Node tempNode = first; first.data = null; first.next = null; first = tempNode.next; return tempNode; } // 在任意位置插入節點 在index的后面插入 public void add(int index, Object data) { if (!(index <= size)) { return; } if (index == 0) { addFirstNode(data); return; } if (index == size) { addTailNode(data); return; } Node node = new Node(data); Node current = first;// 前一個 Node previous = first;// 后一個 while (pos != index) { if (current.next == null) { return; } previous = current; current = current.next; pos++; } previous.next = node; node.next = current; size++; pos = 0; } // 刪除任意位置的節點 public Node deleteByPos(int index) { if (index < 0 || index >= size) { return null; } if (size == 0 || first == null) { return null; } if (index == 0 && size == 1) { Node node = first; first.next = null; first.data = null; first = null; return node; } Node current = first; Node previous = first; while (pos != index) { if (current.next == null) { return null; } previous = current; current = current.next; pos++; } if (current == first) { first = first.next; pos = 0; size--; } else if (index == (size - 1)) { current.data = null; current.next = null; previous.next = null; pos = 0; size--; } else { previous.next = current.next; pos = 0; size--; } return current; } // 根據節點的data刪除節點(僅僅刪除第一個) public Node deleteByData(Object data) { Node current = first; Node previous = first; // 記住上一個節點 while (current.data != data) { if (current.next == null) { return null; } previous = current; current = current.next; } if (current == first) { deleteFirstNode(); } else if (current.next == null) { current.data = null; current.next = null; previous.next = null; size--; } else { previous.next = current.next; size--; } return current; } // 顯示出所有的節點信息 public void displayAllNodes() { Node current = first; while (current != null) { current.display(); current = current.next; } System.out.println(); } // 根據位置查找節點信息 public Node findByPos(int index) { if (index < 0 || index >= size) { return null; } Node current = first; if (pos != index) { current = current.next; pos++; } return current; } // 根據數據查找節點信息 public Node findByData(Object data) { Node current = first; while (current.data != data) { if (current.next == null) return null; current = current.next; } return current; }}class Node { protected Node next; // 指針域 public Object data;// 數據域 public Node(Object data) { this.data = data; } // 顯示此節點 public void display() { System.out.print(data + " "); }}2.循環鏈表的實現package com.sun;public class DcycleLink {public static void main(String[] args) {DcycleLink link = new DcycleLink();link.addTail("張樂");link.addHead("sunchaung");link.addHead("哈哈");link.print();}private Node head;private Node tail;private int count;public DcycleLink() {}public void print() {Node node = head;for (int i = 0; i < count; i++) {System.out.print(" "+node.element);node = node.next;}}// 循環鏈表的末尾增加節點public Object addTail(String element) {if (null == head) {Node node = new Node(element, null, null);head = node;tail = node;head.next = tail;head.prev = tail;tail.next = head;tail.prev = head;count += 1;} else {Node tmp = new Node(element, null, null);tail.next = tmp;tmp.prev = tail;tail = tmp;tail.next = head;head.prev = tail;count += 1;}return element;}// 循環鏈表的頭節點增加節點public Object addHead(String element) {if (null == head) {Node node = new Node(element, null, null);head = node;tail = node;head.next = tail;head.prev = tail;tail.next = head;tail.prev = head;count += 1;} else {Node tmp = new Node(element, null, null);tmp.next = head;head.prev = tmp;head = tmp;head.prev = tail;tail.next = head;count += 1;}return element;}// 向已知位置添加節點public Object addNext(String element, int index) {if (index < 0) {throw new IllegalArgumentException("index can't smaller than 0.");}if (null == head && index > 0) {throw new RuntimeException("link is empty add nothing.");}if (null == head && 0 == index) {return addTail(element);}if (null != head && index > 0) {if (index == count) {Node tmpNode = searchNext(index);Node tmpNextNode = tmpNode.next;Node newNode = new Node(element, null, null);tmpNode.next = newNode;newNode.prev = tmpNode;newNode.next = tmpNextNode;tmpNextNode.prev = newNode;tail = newNode;} else {Node tmpNode = searchNext(index);Node tmpNextNode = tmpNode.next;Node newNode = new Node(element, null, null);tmpNode.next = newNode;newNode.prev = tmpNode;newNode.next = tmpNextNode;tmpNextNode.prev = newNode;}}return element;}// 刪除指定節點public Object delete(final int index) {if (index < 0) {throw new IllegalArgumentException("index can't smaller than 0.");} else { // >=0if (0 == index) {return deleteHead();} else { // index>0if (index >= count) {int tmpIndex = index % count;if (tmpIndex == count - 1) {Node tmpNode = searchNext(tmpIndex);tmpNode.prev.next = tmpNode.next;tmpNode.next.prev = tmpNode.prev;tmpNode.prev = null;tmpNode.next = null;return tmpNode.element;} else {Node tmpNode = searchNext(tmpIndex);Node tmpPreNode = tmpNode.prev;tmpNode.prev.next = tmpNode.next;tmpNode.next.prev = tmpNode.prev;tmpNode.prev = null;tmpNode.next = null;tail = tmpPreNode;return tmpNode.element;}} else { // 0<index<countif (index == count - 1) {Node tmpNode = searchNext(index);tmpNode.prev.next = tmpNode.next;tmpNode.next.prev = tmpNode.prev;tmpNode.prev = null;tmpNode.next = null;return tmpNode.element;} else {Node tmpNode = searchNext(index);Node tmpPreNode = tmpNode.prev;tmpNode.prev.next = tmpNode.next;tmpNode.next.prev = tmpNode.prev;tmpNode.prev = null;tmpNode.next = null;tail = tmpPreNode;return tmpNode.element;}}}}}// 刪除頭節點public Object deleteHead() {if (null == head) {throw new RuntimeException("link is empty, delete nothing.");} else {Object del = head.element;Node tmp = head.next;head.next = null;head.prev = null;tmp.prev = null;head = tmp;head.prev = tail;tail.next = head;count -= 1;return del;}}// 刪除尾節點public Object deleteTail() {if (null == head) {throw new RuntimeException("link is empty, delete nothing.");} else {Object del = tail.element;// 臨時節點是為節點的前一個節點Node tmp = tail.prev;// 尾節點的前一個節點要斷開tail.prev = null;// 尾節點的下一個解釋要斷開tail.next = null;head.prev = null;// 臨時節點的下一個節點要斷開tmp.next = null;// 尾節點是臨時節點tail = tmp;head.prev = tail;tail.next = head;count -= 1;return del;}}// 查詢節點的值private Node searchNext(int index) {if (null == head) {throw new RuntimeException("link is empty, find nothing.");} else {Node tmphead = head;for (int i = 0; i < index; i++) {tmphead = tmphead.next;}return tmphead;}}// 查詢節點的位置public int search(String element) {if (null == head) {throw new RuntimeException("link is empty, delete nothing.");} else {Node tmphead = head;Node tmptail = tail;for (int i = 0; i <= count / 2; i++) {if (element.equals(tmphead.element)) {return i;}if (element.equals(tmptail.element)) {return count - 1 - i;}tmphead = tmphead.next;tmptail = tmptail.prev;}return -1;}}// 節點的長度public int size() {return count;}private class Node {private Object element;private Node next;private Node prev;public Node(String pelement, Node pnext, Node pprev) {this.element = pelement;this.next = pnext;this.prev = pprev;}}}
新聞熱點
疑難解答