Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)
臨近春節(jié),項(xiàng)目都結(jié)束了,都等著回家過(guò)年了。下面是小編給大家研究數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),鏈表算是經(jīng)常用到的一種數(shù)據(jù)結(jié)構(gòu)了,現(xiàn)將自己的實(shí)現(xiàn)展示如下,歡迎大神賜教。
第一個(gè)版本,沒有最后一個(gè)節(jié)點(diǎn),每次從根節(jié)點(diǎn)開始遍歷
public class LinkedList<E> {
private Node head;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=head;
if(lst==null){
this.head=new Node(e, null, null);
return this;
}else{
while(true){
if(lst.next==null){
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除這個(gè)元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**節(jié)點(diǎn)信息*/
private class Node{
public Node pre;
public E value;
public Node next;
public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
}
}
第二個(gè)版本,有了最后一個(gè)節(jié)點(diǎn)
public class LinkedList<E> {
private Node head;
private Node last;
public LinkedList() {
}
public E getFirst(){
if(head==null){
return null;
}
return head.value;
}
public E getLast(){
if(last==null){
return null;
}
return last.value;
}
public LinkedList<E> addFirst(E e){
head.pre=new Node(e, null, head);
head=head.pre;
return this;
}
public LinkedList<E> addNode(E e){
Node lst=last;
if(lst==null){//如果最后一個(gè)節(jié)點(diǎn)是空的則這個(gè)鏈表就是空的
this.last=new Node(e, null, null);
this.head=this.last;
return this;
}else{
while(true){
if(lst.next==null){//
break;
}else{
lst=lst.next;
}
}
lst.next=new Node(e, lst, null);
last=lst.next;
return this;
}
}
public LinkedList<E> remove(E e){
Node lst=head;
if(lst==null){
throw new NullPointerException("the LinkedList is empty.");
}else{
while(true){
if(e.equals(lst.value)){
//移除這個(gè)元素
if(lst.pre!=null){
lst.pre.next=lst.next;
}
if(lst.next!=null){
lst.next.pre=lst.pre;
}
lst=null;
break;
}
lst=lst.next;
}
return this;
}
}
@Override
public String toString() {
StringBuffer buff=new StringBuffer("[");
Node lst=this.head;
while(lst!=null){
buff.append(lst.value+",");
lst=lst.next;
}
return buff.substring(0, buff.length()-1)+"]";
}
/**節(jié)點(diǎn)信息*/
private class Node{
public Node pre;
public E value;
public Node next;
public Node(E value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
}
}
注:以上兩個(gè)版本都沒有考慮在多線程下使用的情況。
以上所述是小編給大家介紹的Java實(shí)現(xiàn)雙向鏈表(兩個(gè)版本)的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例
- Java中雙向鏈表詳解及實(shí)例
- java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解
- 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)文章
SpringBoot整合Docker實(shí)現(xiàn)一次構(gòu)建到處運(yùn)行的操作方法
本文講解的是 SpringBoot 引入容器化技術(shù) Docker 實(shí)現(xiàn)一次構(gòu)建到處運(yùn)行,包括鏡像構(gòu)建、Docker倉(cāng)庫(kù)搭建使用、Docker倉(cāng)庫(kù)可視化UI等內(nèi)容,需要的朋友可以參考下2022-10-10
SpringBoot項(xiàng)目實(shí)現(xiàn)統(tǒng)一異常處理的最佳方案
在前后端分離的項(xiàng)目開發(fā)過(guò)程中,我們通常會(huì)對(duì)數(shù)據(jù)返回格式進(jìn)行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時(shí)同樣會(huì)使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項(xiàng)目中實(shí)現(xiàn)統(tǒng)一異常處理,如有錯(cuò)誤,還望批評(píng)指正2024-02-02
SpringBoot使用mybatis-plus分頁(yè)查詢無(wú)效的問題解決
MyBatis-Plus提供了很多便捷的功能,包括分頁(yè)查詢,本文主要介紹了SpringBoot使用mybatis-plus分頁(yè)查詢無(wú)效的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
解決gateway報(bào)netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemor
這篇文章主要介紹了解決gateway報(bào)netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了mybatis+springboot發(fā)布postgresql數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
JAVA匿名內(nèi)部類(Anonymous Classes)的具體使用
本文主要介紹了JAVA匿名內(nèi)部類,匿名內(nèi)部類在我們JAVA程序員的日常工作中經(jīng)常要用到,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
使用WebSocket實(shí)現(xiàn)即時(shí)通訊(一個(gè)群聊的聊天室)
這篇文章主要為大家詳細(xì)介紹了使用WebSocket實(shí)現(xiàn)即使通訊,實(shí)現(xiàn)一個(gè)群聊的聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

