您好,登錄后才能下訂單哦!
鏈表分為單鏈表,雙向鏈表和循環鏈表,是一種鏈式存儲結構,由一個個結點鏈式構成,結點包含數據域和指針域,其中單鏈表是只有一個指向后驅結點的指針,雙向鏈表除頭結點和尾結點外,每個結點都有一個前驅指針和一個后繼指針,循環鏈表的尾結點的指針指向頭結點.
相比數組而言,鏈表的插入和刪除比較快,查詢慢.
本文主要以單鏈表為例,介紹下鏈表的常用算法操作.
單鏈表的結構:
在java語言中,鏈表的每個結點用Node類來表示:
package com.linkedlist; public class Node { private int data;// 結點數據 private Node next;// 下一個結點 public Node(int data) { this.data = data; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } }
定義一個鏈表操作類,里面包含常用的操作:
package com.linkedlist; import java.util.Hashtable; public class LinkedListOperator { private Node head = null;// 頭結點 // 在鏈表的末尾增加一個結點 private void addNode(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.getNext() != null) { temp = temp.getNext(); } temp.setNext(newNode); } // 打印鏈表結點 private void printLink() { Node curNode = head; while (curNode != null) { System.out.println(curNode.getData()); curNode = curNode.getNext(); } System.out.println("==========="); } // 求鏈表長度 private int getLength() { int len = 0; Node curNode = head; while (curNode != null) { len++; curNode = curNode.getNext(); } return len; } // 刪除某一個結點 private boolean delNode(int index) { if (index < 1) { return false; } if (index == 1) { head = head.getNext(); return true; } Node preNode = head; Node curNode = head.getNext(); int n = 1; while (curNode.getNext() != null) { if (n == index) { preNode.setData(curNode.getData()); preNode.setNext(curNode.getNext()); return true; } preNode = preNode.getNext(); curNode = curNode.getNext(); n++; } if (curNode.getNext() == null) { preNode.setNext(null); } return false; } // 鏈表排序:選擇排序法,從小到大 private void sortList() { Node curNode = head; while (curNode != null) { Node nextNode = curNode.getNext(); while (nextNode != null) { if (curNode.getData() > nextNode.getData()) { int temp = curNode.getData(); curNode.setData(nextNode.getData()); nextNode.setData(temp); } nextNode = nextNode.getNext(); } curNode = curNode.getNext(); } } // 去掉重復元素 private void distinctLink() { Hashtable<Integer, Integer> map = new Hashtable<Integer, Integer>(); Node curNode = head; Node preNode = null; while (curNode != null) { if (map.containsKey(curNode.getData())) { preNode.setData(curNode.getData()); preNode.setNext(curNode.getNext()); } else { map.put(curNode.getData(), 1); preNode = curNode; } curNode = curNode.getNext(); } } // 返回倒數第k個結點,定義兩個指針,第一個指針向前移動K-1次,之后兩個指針同時前進, // 當第一個指針到達末尾時,第二個指針所在的位置即為倒數第k個結點 private Node getReverNode(int k) { if (k < 1) { return null; } Node first = head; Node second = head; for (int i = 0; i < k - 1; i++) { first = first.getNext(); } while (first.getNext() != null) { first = first.getNext(); second = second.getNext(); } return second; } // 反轉鏈表 private void reserveLink() { Node preNode = null; Node curNode = head; Node tempNode = null; while (curNode != null) { tempNode = curNode.getNext(); curNode.setNext(preNode); preNode = curNode; curNode = tempNode; } head = preNode; } // 尋找鏈表的中間結點 private Node getMiddleNode() { Node slowNode = head; Node quickNode = head; while (slowNode.getNext() != null && quickNode.getNext() != null) { slowNode = slowNode.getNext(); quickNode = quickNode.getNext().getNext(); } return slowNode; } // 判斷鏈表是否有環 private boolean isRinged() { if (head == null) { return false; } Node slowNode = head; Node quickNode = head; while (slowNode.getNext() != null && quickNode.getNext() != null) { slowNode = slowNode.getNext(); quickNode = quickNode.getNext().getNext(); if (slowNode.getData() == quickNode.getData()) { return true; } } return false; } // 刪除指定結點 private boolean delNode(Node node) { if (node.getNext() == null) { return false;// 在不知道頭結點的情況下,沒法刪除單鏈表的尾結點 } node.setData(node.getNext().getData()); node.setNext(node.getNext().getNext()); return true; } // 判斷兩個鏈表是否相交:相交的鏈表的尾結點相同 private boolean isCross(Node n1, Node n2) { while (n1.getNext() != null) { n1 = n1.getNext(); } while (n2.getNext() != null) { n2 = n2.getNext(); } if (n1.getData() == n2.getData()) { return true; } return false; } // 求相交鏈表的起始點 private Node getFirstCrossNode(LinkedListOperator l1, LinkedListOperator l2) { int len = l1.getLength() - l2.getLength(); Node n1 = l1.head; Node n2 = l2.head; if (len > 0) { for (int i = 0; i < len; i++) { n1 = n1.getNext(); } } else { for (int i = 0; i < len; i++) { n2 = n2.getNext(); } } while (n1.getData() != n2.getData()) { n1 = n1.getNext(); n2 = n2.getNext(); } return n1; } public static void main(String[] args) { LinkedListOperator llo = new LinkedListOperator(); llo.addNode(10); llo.addNode(4); llo.addNode(6); llo.addNode(8); llo.printLink(); // llo.delNode(4); // llo.sortList(); // llo.distinctLink(); // System.out.println(llo.getReverNode(3).getData()); // llo.reserveLink(); // System.out.println(llo.getMiddleNode().getData()); // System.out.println(llo.isRinged()); llo.delNode(llo.head.getNext().getNext()); llo.printLink(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。