class List { Node head; //指向鏈表頭部 public List() { clearList(); } /** * 清空鏈表信息 */ public void clearList() { head = null; } /** * 獲取鏈表信息 */ public void getList() { Node cur = head; while (cur != null) { System.out.PRint(cur.data + ","); cur = cur.next; } System.out.println(); } /** * 添加結點到鏈表底部 * @param data */ public void addNode(int data) { Node node = new Node(data); Node pro = head; Node cur = head; if (cur == null) { node.next = null; head = node; } else { while (cur != null) { pro = cur; cur = cur.next; } cur = node; pro.next = cur; } } /** * 指定位置將結點插入到鏈表中 * @param pos * @param data */ public void addNode(int pos, int data) { int index; Node node = new Node(data); Node cur = head; if (pos < 0 || pos > this.getLength() - 1) { System.out.println("操作失敗,超出鏈表長度!"); return; } else { for (index = 0; index < pos; index++) { cur = cur.next; } if (cur == null) { cur = node; } else { node.next = cur.next; cur.next = node; } } } /** * 獲取鏈表長度 * @return */ public int getLength() { int length = 0; Node cur = head; if (cur == null) { return 0; } else { while (cur != null) { length++; cur = cur.next; } return length; } } /** * 刪除鏈表中結點信息 * @param data */ public void deleteNode(int data) { int index = searchNode(data); int del = 0; Node pro = head; Node cur = head; if (index < 0) { System.out.println("操作失敗,沒有找到 " + data); return; } else if (index == 0) { if(cur.next==null){ head=null; }else { head=head.next; } } else { while (del != index) { pro = cur; if (cur.next != null) { cur = cur.next; } del++; } pro.next = cur.next; cur = cur.next; } System.out.println("操作成功!刪除了 " + data); } /** * 查找鏈表中的結點,并返回其所在位置 * @param data * @return */ public int searchNode(int data) { int index = 0; Node cur = head; while (cur != null) { if (cur.data == data) { return index; } else { cur = cur.next; index++; } } return -1; } /** * 結點類 * @author wky653 * */ class Node { int data; Node next; public Node(int data) { this.data = data; } public Node() { } }}/** * 測試類 * @author wky653 * */public class Test01 { public static void main(String[] args) { List list = new List(); list.addNode(144); list.addNode(15); list.addNode(14); list.addNode(13); list.addNode(3, 12); list.getList(); System.out.println("length=" + list.getLength()); list.deleteNode(12); list.getList(); list.clearList(); list.getList(); }}運行結果:
144,15,14,13,12,length=5操作成功!刪除了 12144,15,14,13,2.雙向鏈表
//結點數據class Node { int data; // 數據域 Node back; // 上一個指針域 Node next; // 下一個指針域 public Node(int data) { this.data = data; }}// 雙向鏈表的具體操作class TwoList { Node head; // 保存頭節點信息 public TwoList() { head = null; } /** * 打印鏈表所有結點信息 */ public void getList() { Node cur = head; while (cur != null) { System.out.print(cur.data + ","); cur = cur.next; } System.out.println(); } /** * 添加在鏈表頭部 * * @param data */ public void addToHead(int data) { Node node = new Node(data); if (head == null) { head = node; } else { node.next = head; head.back = node; head = node; } } /** * 添加在鏈表尾部 * * @param data */ public void addToTail(int data) { Node node = new Node(data); Node pro = head; Node cur = head; if (head == null) { head = node; } else { while (cur != null) { pro = cur; cur = cur.next; } cur = node; cur.back = pro; pro.next = cur; } } /** * 在指定位置插入新的節點 * * @param pos * @param data */ public void addNode(int pos, int data) { int index = 0; Node node = new Node(data); Node pro = head; Node cur = head; if (pos < 0 || pos > this.getLength() - 1) { System.out.println("超出鏈表長度,添加" + data + "失敗!"); return; } for (; index <= pos; index++) { if (cur != null) { pro = cur; cur = cur.next; } else { System.out.println("添加" + data + "失敗!"); return; } } pro.next = node; node.back = pro; node.next = cur; if (cur != null) { cur.back = node; } } /** * 獲取鏈表長度 * * @return */ public int getLength() { Node cur = head; int length = 0; if (cur == null) { return -1; } while (cur != null) { length++; cur = cur.next; } return length; } /** * 刪除鏈表中指定信息的結點 * * @param data */ public void deleteNode(int data) { int flag = searchNode(data); int index; Node pro = head; Node cur = head; if (flag < 0) { System.out.println(data + "不存在,刪除操作失敗!"); } else if (flag == 0) { head.next.back = null; head = head.next; } else { for (index = 0; index < flag; index++) { pro = cur; cur = cur.next; } pro.next = cur.next; cur = cur.next; // cur.back=pro; } } /** * 刪除指定位置的結點 * * @param pos * @param tl */ public void deleteNode(int pos, TwoList tl) { int index; Node pro = head; Node cur = head; if (pos < 0 || pos > tl.getLength() - 1) { System.out.println("超出數組長度,刪除操作失敗!"); } else if (pos == 0) { head.next.back = null; head = head.next; } else { for (index = 0; index < pos; index++) { pro = cur; cur = cur.next; } if (cur != null) { pro.next = cur.next; cur = cur.next; if (cur != null) { cur.back = pro; } } else { System.out.println("刪除失敗!"); } } } /** * 返回指定信息的結點位置 * * @param data * @return */ public int searchNode(int data) { int count = 0; Node cur = head; while (cur != null) { if (cur.data == data) { return count; } else { count++; cur = cur.next; } } return -1; }}/** * 測試類 * * @author wky653 * */public class Test02 { public static void main(String[] args) { TwoList list = new TwoList(); list.addToHead(18); list.addToHead(14); list.addToHead(11); list.addToHead(48); list.addToTail(20); list.addToTail(22); list.addNode(-1, 10); list.getList(); System.out.println(list.getLength()); System.out.println(list.searchNode(14)); list.deleteNode(20); list.getList(); list.deleteNode(4, list); list.getList(); }}運行結果:
超出鏈表長度,添加10失敗!48,11,14,18,20,22,6248,11,14,18,22,48,11,14,18,3.雙向循環鏈表
class ThreeList { Node head; Node tail; public ThreeList() { this.clearList(); } /** * 清空鏈表 */ public void clearList() { head = tail = null; } /** * 讀取完整鏈表信息 */ public void getList() { Node cur = head; if (cur == null) { System.out.println("鏈表為空!"); return; } while (cur != tail) { System.out.print(cur.data + ","); cur = cur.next; } System.out.print(cur.data); System.out.println(); } /** * 返回鏈表長度 * * @return */ public int getLength() { int length; Node cur = head; if (cur == null) { return 0; } for (length = 0; cur != tail; length++) { cur = cur.next; } return length + 1; } /** * 往鏈表頭部插入結點 * * @param data */ public void addHeadNode(int data) { Node node = new Node(data); if (head == null) { head = node; tail = head; } else if (head == tail) { head = node; head.next = tail; head.back = tail; tail.next = head; tail.back = head; } else { node.next = head; head.back = node; head = node; head.back = tail; tail.next = head; } System.out.println("添加" + data + "成功!"); } /** * 往鏈表尾部插入結點 * * @param data */ public void addTailNode(int data) { Node node = new Node(data); if (head == null) { head = new Node(data); tail = head; } else if (head == tail) { tail = node; tail.back = head; tail.next = head; head.next = tail; head.back = tail; } else { tail.next = node; node.back = tail; tail = node; tail.next = head; head.back = tail; } System.out.println("添加" + data + "成功!"); } /** * 指定位置插入結點 * * @param pos * @param data */ public void addToNode(int pos, int data) { int index = 0; Node node = new Node(data); Node cur = head; Node pro = head; if (pos > this.getLength()) { System.out.println("操作失敗,超出鏈表長度!"); return; } else if (pos < 0) { addHeadNode(data); // 往鏈表頭部插入 return; } else { for (; index <= pos; index++) { pro = cur; cur = cur.next; } pro.next = node; node.back = pro; node.next = cur; cur.back = node; pro = node; } System.out.println("添加" + data + "成功!"); } /** * 刪除指定信息的結點 * * @param data */ public void deleteNode(int data) { int index = searchNode(data); int del = 0; Node pro = head; Node cur = head; if (index < 0) { System.out.println("操作失敗,沒有找到 " + data); return; } else if (index == 0) { // 目標是鏈表頭部結點 head = head.next; head.back = tail; tail.next = head; } else if (index == this.getLength() - 1) { // 目標是鏈表尾部結點 tail.back.next = head; head.back = tail.back; tail = tail.back; } else { while (del != index) { pro = cur; if (cur.next != null) { cur = cur.next; } del++; } pro.next = cur.next; cur.next.back = pro; cur.back = cur.next = null; } System.out.println("操作成功!刪除了 " + data); } /** * 查找指定信息的結點,并返回其所處位置 * * @param data * @return */ public int searchNode(int data) { int index = 0; Node cur = head; if (cur == null) { // 空鏈表 return -1; } else if (cur == tail) { // 鏈表只有一個結點 if (cur.data == data) { return index; } else return -1; } while (cur != tail) { if (cur.data == data) { return index; } else { cur = cur.next; index++; } } if (cur.data == data) { // 判斷鏈表尾部 return index++; } else { return -1; } } /** * 結點類 * * @author wky653 * */ class Node { int data; Node next; Node back; public Node(int data) { this.data = data; } public Node() { } }}/** * 測試類 * * @author wky653 * */public class Test03 { public static void main(String[] args) { ThreeList list = new ThreeList(); list.addTailNode(14); list.addTailNode(21); list.addHeadNode(20); list.addToNode(5, 30); list.getList(); System.out.println("length=" + list.getLength()); list.addTailNode(50); list.getList(); list.deleteNode(50); list.getList(); list.deleteNode(15); list.getList(); System.out.println("length=" + list.getLength()); list.clearList(); list.getList(); System.out.println("length=" + list.getLength()); }}運行結果:
添加14成功!添加21成功!添加20成功!操作失敗,超出鏈表長度!20,14,21length=3添加50成功!20,14,21,50操作成功!刪除了 5020,14,21操作失敗,沒有找到 1520,14,21length=3鏈表為空!length=0
新聞熱點
疑難解答