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

