java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解
java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解
雙向鏈表是一個(gè)基本的數(shù)據(jù)結(jié)構(gòu),在Java中LinkedList已經(jīng)實(shí)現(xiàn)了這種結(jié)構(gòu),不過(guò)作為開(kāi)發(fā)者,也要擁有自己顯示這種結(jié)構(gòu)的能力。話不多說(shuō),上代碼:
首先是鏈表的節(jié)點(diǎn)類:
/**
* 鏈表節(jié)點(diǎn)
* @author Administrator
*
* @param <T>
*/
public class ChainNode<T> {
private T data;
//對(duì)象編號(hào)
private int dataNo;
public ChainNode<T> nextChainNode;
public ChainNode<T> preChainNode;
public ChainNode(T data, ChainNode<T> nextChainNode,
ChainNode<T> preChainNode) {
this.data = data;
this.nextChainNode = nextChainNode;
this.preChainNode = preChainNode;
}
public ChainNode(T data) {
this.data = data;
}
public int getDataNo() {
return dataNo;
}
public void setDataNo(int dataNo) {
this.dataNo = dataNo;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
然后是鏈表:
<pre name="code" class="java">/**
* 鏈表實(shí)現(xiàn)類
* @author Administrator
*
* @param <T>
*/
public class Chain<T> {
//頭節(jié)點(diǎn)
private ChainNode<T> headNode;
//末尾節(jié)點(diǎn)指針
private ChainNode<T> lastNode;
private int size;
//創(chuàng)建鏈表就創(chuàng)建頭節(jié)點(diǎn)
public Chain() {
this.headNode = new ChainNode<T>(null);
this.lastNode = headNode;
}
//增加一個(gè)節(jié)點(diǎn)
public void addNode(T data) {
ChainNode<T> node = new ChainNode<T>(data);
if(lastNode != null){
lastNode.nextChainNode = node;
node.preChainNode = node;
node.setDataNo(size);
lastNode = node;
size++;
}
}
//刪除指定索引的節(jié)點(diǎn)
public void deleteNode(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.preChainNode.nextChainNode = node.nextChainNode;
if(node.nextChainNode != null){
node.nextChainNode.preChainNode = node.preChainNode;
}
node.nextChainNode = null;
node.preChainNode = null;
size--;
//重新設(shè)置節(jié)點(diǎn)的編號(hào)
for (ChainNode<T> chainNode = node.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
chainNode.setDataNo(chainNode.getDataNo()-1);
}
return;
}
}
throw new Exception("the corresponding data that can not be found");
}
//獲取指定索引的節(jié)點(diǎn)
public T get(int dataNo) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
return node.getData();
}
}
throw new Exception("the corresponding data that can not be found");
}
//修改對(duì)應(yīng)索引的節(jié)點(diǎn)
public void set(int dataNo,T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) {
if(node.getDataNo() == dataNo){
node.setData(data);
return;
}
}
throw new Exception("the data that is to be modified can not be found");
}
//是否包含對(duì)應(yīng)數(shù)據(jù)的節(jié)點(diǎn)
public boolean isContains(T data) throws Exception {
if(getSize() == 0){
throw new Exception("chain is empty");
}
for (ChainNode<T> chainNode = headNode.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) {
if(chainNode.getData() == data){
return true;
}
}
return false;
}
//獲取節(jié)點(diǎn)數(shù)量(不含頭節(jié)點(diǎn))
public int getSize() {
return size;
}
}
測(cè)試一下:
public class ChainTest {
public static void main(String[] args) throws Exception{
String oneString = "one";
String twoString = "two";
String threeString = "three";
String fourString = "four";
Chain<String> chain = new Chain<String>();
chain.addNode(oneString);
chain.addNode(twoString);
chain.addNode(threeString);
chain.addNode(fourString);
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
try {
String string = chain.get(2);
System.out.println("the data of the second node is"+string);
System.out.println(chain.isContains(fourString));
chain.set(1, "haha");
System.out.println("modify chain");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
chain.deleteNode(3);
System.out.println("delete one node");
for (int i = 0; i < chain.getSize(); i++) {
String string2 = chain.get(i);
System.out.println(string2);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
結(jié)果:
one two three four the data of the second node isthree true modify chain one haha three four delete one node one haha three
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例
- Java中雙向鏈表詳解及實(shí)例
- Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)
- Java語(yǔ)言中鏈表和雙向鏈表
- JAVA實(shí)現(xiàn)雙向鏈表的增刪功能的方法
- java中使用雙向鏈表實(shí)現(xiàn)貪吃蛇程序源碼分享
- java實(shí)現(xiàn)單鏈表、雙向鏈表
- Java雙向鏈表按照順序添加節(jié)點(diǎn)的方法實(shí)例
- java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):單鏈表與雙向鏈表
- 基于Java實(shí)現(xiàn)雙向鏈表
相關(guān)文章
java 實(shí)現(xiàn)圖片圓角處理、背景透明化
這篇文章主要介紹了java 實(shí)現(xiàn)圖片圓角處理、背景透明化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
關(guān)于fastjson的@JSONField注解的一些問(wèn)題(詳解)
下面小編就為大家?guī)?lái)一篇關(guān)于fastjson的@JSONField注解的一些問(wèn)題(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Java實(shí)現(xiàn)FIFO任務(wù)調(diào)度隊(duì)列策略
在工作中,很多高并發(fā)的場(chǎng)景中,我們會(huì)用到隊(duì)列來(lái)實(shí)現(xiàn)大量的任務(wù)請(qǐng)求。當(dāng)任務(wù)需要某些特殊資源的時(shí)候,我們還需要合理的分配資源,讓隊(duì)列中的任務(wù)高效且有序完成任務(wù)。本文將為大家介紹通過(guò)java實(shí)現(xiàn)FIFO任務(wù)調(diào)度,需要的可以參考一下2021-12-12
springboot簡(jiǎn)單接入websocket的操作方法
這篇文章主要介紹了springboot簡(jiǎn)單接入websocket的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
SpringBoot中@EnableAutoConfiguration注解源碼分析
這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration注解源碼分析,@EnableAutoConfiguration,主要是用于加載Starter目錄包之外的、需要Spring自動(dòng)生成Bean對(duì)象的、帶有@Configuration注解的類,需要的朋友可以參考下2023-08-08
springboot項(xiàng)目編寫(xiě)發(fā)送異常日志到企微工具包的操作方法
本文介紹了Springboot項(xiàng)目如何編寫(xiě)發(fā)送異常日志到企業(yè)微信的工具包,內(nèi)容包括創(chuàng)建基礎(chǔ)Bean、配置類、pom依賴等步驟,并展示了如何通過(guò)nacos進(jìn)行配置,這為開(kāi)發(fā)者提供了一種有效的日志管理方案,方便快速定位和處理項(xiàng)目中的異常問(wèn)題,感興趣的朋友跟隨小編一起看看吧2024-09-09
Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟
這篇文章主要介紹了Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解
這篇文章主要介紹了Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解,Spring的設(shè)計(jì)非常優(yōu)雅,有很多的擴(kuò)展點(diǎn)供我們對(duì)項(xiàng)目進(jìn)行擴(kuò)展,今天學(xué)習(xí)一下Spring其中擴(kuò)展點(diǎn)之一的BeanFactoryPostProcessor,需要的朋友可以參考下2023-11-11

