【單鏈表】快慢指標原理-Java語言
阿新 • • 發佈:2018-12-30
1.結點類
package blog;
/**
* @Description: 結點類
* @author: liuqiang
* @Date: 2018/12/28 23:33
*/
public class Node {
public String data; // 結點的資料域
public Node next; // 結點的指標域
public Node() {
}
// 構造方法時給data賦值
public Node(String data) {
this.data = data;
}
}
2.單鏈表類
package bolg;
/**
* @Description: 單鏈表類
* @author: liuqiang
* @Date: 2018/12/28 23:38
*/
public class LinkList {
private Node head; // 頭結點
// 呼叫LinkList構造方法時初始化head
public LinkList() {
head = new Node();
}
/**
* @Description: 尾插法插入結點
* @param: [node]
* @return: void
* @author: liuqiang
* @updateTime: 2018/12/28 23:53
*/
public void addNode(Node node) {
Node temp = head; // 把頭結點看做一個指向結點的指標
while (temp.next != null) { // 遍歷單鏈表,直到遍歷最後的元素則跳出
temp = temp.next; // 指標指向下一個結點
}
temp.next = node; // 將node指向最後一個結點
}
/**
* @Description: 計算連結串列的長度
* @param: []
* @return: int
* @author: liuqiang
* @updateTime: 2018/12/28 23:58
*/
public int length() {
int length = 0;
Node temp = head;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
/**
* @Description: 在指定位置插入結點
* @param: [index, node]
* @return: void
* @author: liuqiang
* @updateTime: 2018/12/30 14:29
*/
public void insert(int index, Node node) {
if (index < 1 || index > length() + 1) {
System.out.println("插入位置不合法");
return;
}
int length = 1; // 記錄遍歷到的位置
Node temp = head; // 可移動的指標
while (head.next != null) { // 遍歷單鏈表
if (index == length) {
node.next = temp.next;
temp.next = node;
return;
}
temp = temp.next;
}
}
/**
* @Description: 根據位序數刪除結點
* @param: [index]
* @return: void
* @author: liuqiang
* @updateTime: 2018/12/30 14:44
*/
public void delete(int index) {
// 判斷index是否合理
if (index < 0 || index > length()) {
System.out.println("刪除位置不合理");
return;
}
int length = 1;
Node temp = head;
while (temp.next != null) {
if (index == length) {
temp.next = temp.next.next;
return;
}
temp = temp.next;
length++;
}
}
/**
* @Description: 列印單鏈表的資料
* @param: []
* @return: void
* @author: liuqiang
* @updateTime: 2018/12/30 19:52
*/
public void print() {
Node temp = head.next;
for (int i = 1; i <= length(); i++) {
System.out.println("第" + i + "個結點的資料為" + temp.data);
temp = temp.next;
}
}
/**
* @Description: 獲取中間結點的資料
* @param: [linkList]
* @return: void
* @author: liuqiang
* @updateTime: 2018/12/30 19:14
*/
public void getMidNode(LinkList linkList) {
Node search = linkList.head; // 定義一個快結點
Node mid = linkList.head; // 定義一個慢結點
while (search.next != null) { // 遍歷單鏈表
if (search.next.next != null) {
// 如果快結點的下下個結點不為空,則說明離最後一個結點至少還有兩個結點,往後走兩個結點
search = search.next.next;
} else {
// 如果快結點的下下個結點為空,但下個結點不為空,
// 則說明單鏈表的結點數為奇數,往後走一個結點即到達最後一個結點
search = search.next;
}
mid = mid.next;
}
System.out.println(mid.data);
}
}
3.測試程式碼
package bolg;
/**
* @Description: 測試類
* @author: liuqiang
* @Date: 2018/12/30 14:48
*/
public class Test {
public static void main(String[] args) {
LinkList linkList = new LinkList();
// 往單鏈表中插入多個結點
for (int i = 1; i < 12; i++) {
Node node = new Node("結點" + i);
linkList.addNode(node);
}
linkList.getMidNode(linkList); // 呼叫獲得中間結點資料的函式
}
}
4.輸出結果:結點6