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è)版本,沒(méi)有最后一個(gè)節(jié)點(diǎn),每次從根節(jié)點(diǎn)開(kāi)始遍歷
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è)版本都沒(méi)有考慮在多線(xiàn)程下使用的情況。
以上所述是小編給大家介紹的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)文章
Mybatis和Mybatis-Plus時(shí)間范圍查詢(xún)方式
這篇文章主要介紹了Mybatis和Mybatis-Plus時(shí)間范圍查詢(xún)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Springboot @Value獲取值為空問(wèn)題解決方案
這篇文章主要介紹了Springboot @Value獲取值為空問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02解決IDEA無(wú)法下載maven依賴(lài)的問(wèn)題
這篇文章主要介紹了解決IDEA無(wú)法下載maven依賴(lài)的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09詳解Java中的println輸入和toString方法的重寫(xiě)問(wèn)題
這篇文章主要介紹了Java中的println輸入和toString方法的重寫(xiě),一個(gè)對(duì)象數(shù)組在調(diào)用Arrays.toString打印時(shí),相當(dāng)于遍歷數(shù)組,然后打印里邊每個(gè)對(duì)象,這再打印對(duì)象就調(diào)用對(duì)象自己的toString了,需要的朋友可以參考下2022-04-04解決SpringBoot下Redis序列化亂碼的問(wèn)題
這篇文章主要介紹了解決SpringBoot下Redis序列化亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06