Java中雙向鏈表詳解及實例
Java中雙向鏈表詳解及實例
寫在前面:
雙向鏈表是一種對稱結(jié)構(gòu),它克服了單鏈表上指針單向性的缺點,其中每一個節(jié)點即可向前引用,也可向后引用,這樣可以更方便的插入、刪除數(shù)據(jù)元素。
由于雙向鏈表需要同時維護兩個方向的指針,因此添加節(jié)點、刪除節(jié)點時指針維護成本更大;但雙向鏈表具有兩個方向的指針,因此可以向兩個方向搜索節(jié)點,因此雙向鏈表在搜索節(jié)點、刪除指定索引處節(jié)點時具有較好的性能。
Java語言實現(xiàn)雙向鏈表:
package com.ietree.basic.datastructure.dublinklist;
/**
* 雙向鏈表
*
* @author Dylan
*/
public class DuLinkList<T> {
// 定義一個內(nèi)部類Node,Node實例代表鏈表的節(jié)點
private class Node {
// 保存節(jié)點的數(shù)據(jù)
private T data;
// 保存上個節(jié)點的引用
private Node prev;
// 指向下一個節(jié)點的引用
private Node next;
// 無參構(gòu)造器
public Node() {
}
// 初始化全部屬性的構(gòu)造器
public Node(T data, Node prev, Node next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
// 保存該鏈表的頭節(jié)點
private Node header;
// 保存該鏈表的尾節(jié)點
private Node tail;
// 保存該鏈表中已包含的節(jié)點數(shù)
private int size;
// 創(chuàng)建空鏈表
public DuLinkList() {
// 空鏈表,header和tail都是null
header = null;
tail = null;
}
// 以指定數(shù)據(jù)元素來創(chuàng)建鏈表,該鏈表只有一個元素
public DuLinkList(T element) {
header = new Node(element, null, null);
// 只有一個節(jié)點,header、tail都指向該節(jié)點
tail = header;
size++;
}
// 返回鏈表的長度
public int length() {
return size;
}
// 獲取鏈式線性表中索引為index處的元素
public T get(int index) {
return getNodeByIndex(index).data;
}
// 根據(jù)索引index獲取指定位置的節(jié)點
public Node getNodeByIndex(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("線性表索引越界");
}
if (index <= size / 2) {
// 從header節(jié)點開始
Node current = header;
for (int i = 0; i <= size / 2 && current != null; i++, current = current.next) {
if (i == index) {
return current;
}
}
} else {
// 從tail節(jié)點開始搜索
Node current = tail;
for (int i = size - 1; i > size / 2 && current != null; i++, current = current.prev) {
if (i == index) {
return current;
}
}
}
return null;
}
// 查找鏈式線性表中指定元素的索引
public int locate(T element) {
// 從頭結(jié)點開始搜索
Node current = header;
for (int i = 0; i < size && current != null; i++, current = current.next) {
if (current.data.equals(element)) {
return i;
}
}
return -1;
}
// 向線性鏈表的指定位置插入一個元素
public void insert(T element, int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("線性表索引越界");
}
// 如果還是空鏈表
if (header == null) {
add(element);
} else {
// 當index為0時,也就是在鏈表頭處插入
if (index == 0) {
addAtHeader(element);
} else {
// 獲取插入點的前一個節(jié)點
Node prev = getNodeByIndex(index - 1);
// 獲取插入點的節(jié)點
Node next = prev.next;
// 讓新節(jié)點的next引用指向next節(jié)點,prev引用指向prev節(jié)點
Node newNode = new Node(element, prev, next);
// 讓prev的next節(jié)點指向新節(jié)點
prev.next = newNode;
// 讓prev的下一個節(jié)點的prev指向新節(jié)點
next.prev = newNode;
size++;
}
}
}
// 采用尾插法為鏈表添加新節(jié)點
public void add(T element) {
// 如果該鏈表還是空鏈表
if (header == null) {
header = new Node(element, null, null);
// 只有一個節(jié)點,header、tail都指向該節(jié)點
tail = header;
} else {
// 創(chuàng)建新節(jié)點,新節(jié)點的pre指向原tail節(jié)點
Node newNode = new Node(element, tail, null);
// 讓尾節(jié)點的next指向新增的節(jié)點
tail.next = newNode;
// 以新節(jié)點作為新的尾節(jié)點
tail = newNode;
}
size++;
}
// 采用頭插法為鏈表添加新節(jié)點
public void addAtHeader(T element) {
// 創(chuàng)建新節(jié)點,讓新節(jié)點的next指向原來的header
// 并以新節(jié)點作為新的header
header = new Node(element, null, header);
// 如果插入之前是空鏈表
if (tail == null) {
tail = header;
}
size++;
}
// 刪除鏈式線性表中指定索引處的元素
public T delete(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("線性表索引越界");
}
Node del = null;
// 如果被刪除的是header節(jié)點
if (index == 0) {
del = header;
header = header.next;
// 釋放新的header節(jié)點的prev引用
header.prev = null;
} else {
// 獲取刪除節(jié)點的前一個節(jié)點
Node prev = getNodeByIndex(index - 1);
// 獲取將要被刪除的節(jié)點
del = prev.next;
// 讓被刪除節(jié)點的next指向被刪除節(jié)點的下一個節(jié)點
prev.next = del.next;
// 讓被刪除節(jié)點的下一個節(jié)點的prev指向prev節(jié)點
if (del.next != null) {
del.next.prev = prev;
}
// 將被刪除節(jié)點的prev、next引用賦為null
del.prev = null;
del.next = null;
}
size--;
return del.data;
}
// 刪除鏈式線性表中最后一個元素
public T remove() {
return delete(size - 1);
}
// 判斷鏈式線性表是否為空表
public boolean empty() {
return size == 0;
}
// 清空線性表
public void clear() {
// 將底層數(shù)組所有元素賦為null
header = null;
tail = null;
size = 0;
}
public String toString() {
// 鏈表為空鏈表
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
for (Node current = header; current != null; current = current.next) {
sb.append(current.data.toString() + ", ");
}
int len = sb.length();
return sb.delete(len - 2, len).append("]").toString();
}
}
// 倒序toString
public String reverseToString() {
if (empty()) {
return "[]";
} else {
StringBuilder sb = new StringBuilder("[");
for (Node current = tail; current != null; current = current.prev) {
sb.append(current.data.toString() + ", ");
}
int len = sb.length();
return sb.delete(len - 2, len).append("]").toString();
}
}
}
測試類:
package com.ietree.basic.datastructure.dublinklist;
/**
* 測試類
*
* @author Dylan
*/
public class DuLinkListTest {
public static void main(String[] args) {
DuLinkList<String> list = new DuLinkList<String>();
list.insert("aaaa", 0);
list.add("bbbb");
list.insert("cccc", 0);
// 在索引為1處插入一個新元素
list.insert("dddd", 1);
// 輸出順序線性表的元素
System.out.println(list);
// 刪除索引為2處的元素
list.delete(2);
System.out.println(list);
System.out.println(list.reverseToString());
// 獲取cccc字符串在順序線性表中的位置
System.out.println("cccc在順序線性表中的位置:" + list.locate("cccc"));
System.out.println("鏈表中索引1處的元素:" + list.get(1));
list.remove();
System.out.println("調(diào)用remove方法后的鏈表:" + list);
list.delete(0);
System.out.println("調(diào)用delete(0)后的鏈表:" + list);
}
}
程序輸出:
[cccc, dddd, aaaa, bbbb] [cccc, dddd, bbbb] [bbbb, dddd, cccc] cccc在順序線性表中的位置:0 鏈表中索引1處的元素:dddd 調(diào)用remove方法后的鏈表:[cccc, dddd] 調(diào)用delete(0)后的鏈表:[dddd]
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解Java使用Jsch與sftp服務器實現(xiàn)ssh免密登錄
這篇文章主要介紹了詳解Java使用Jsch與sftp服務器實現(xiàn)ssh免密登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
詳解如何使用Spring的@FeignClient注解實現(xiàn)通信功能
SpringBoot是一個非常流行的Java框架,它提供了一系列工具來使這種交互無縫且高效,在這些工具中,@FeignClient注解因其易用性和強大的功能而脫穎而出, 在這篇文章中,我們將探討如何使用Spring的@FeignClient注解進行客戶端-服務器通信,需要的朋友可以參考下2023-11-11
freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例
這篇文章主要介紹了freemarker?jsp?java內(nèi)存方式實現(xiàn)分頁示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)
這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn),文章首先通過需要先引入坐標展開主題的相關(guān)內(nèi)容介紹,需要的朋友可以參一下2022-06-06
Java基于面向?qū)ο髮崿F(xiàn)一個戰(zhàn)士小游戲
這篇文章主要為大家詳細介紹了Java如何基于面向?qū)ο髮崿F(xiàn)一個戰(zhàn)士小游戲,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下2022-07-07

