欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)雙向鏈表(兩個版本)

 更新時間:2016年02月06日 16:44:28   投稿:mrr  
這篇文章主要介紹了Java實現(xiàn)雙向鏈表(兩個版本)的相關(guā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)文章

  • Mybatis和Mybatis-Plus時間范圍查詢方式

    Mybatis和Mybatis-Plus時間范圍查詢方式

    這篇文章主要介紹了Mybatis和Mybatis-Plus時間范圍查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Springboot @Value獲取值為空問題解決方案

    Springboot @Value獲取值為空問題解決方案

    這篇文章主要介紹了Springboot @Value獲取值為空問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • java將m3u8格式轉(zhuǎn)成視頻文件的方法

    java將m3u8格式轉(zhuǎn)成視頻文件的方法

    這篇文章主要介紹了如何java將m3u8格式轉(zhuǎn)成視頻文件,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Java中使用Filter過濾器的方法

    Java中使用Filter過濾器的方法

    Filter過濾器是javaWeb層面的,它跟Servlet類似,每次前端請求,首先進(jìn)入的是過濾器,我們必須實現(xiàn)Filter接口,重寫三個方法,才能使用Filter過濾器,需要的朋友可以參考下
    2021-06-06
  • 解決IDEA無法下載maven依賴的問題

    解決IDEA無法下載maven依賴的問題

    這篇文章主要介紹了解決IDEA無法下載maven依賴的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解Java中的println輸入和toString方法的重寫問題

    詳解Java中的println輸入和toString方法的重寫問題

    這篇文章主要介紹了Java中的println輸入和toString方法的重寫,一個對象數(shù)組在調(diào)用Arrays.toString打印時,相當(dāng)于遍歷數(shù)組,然后打印里邊每個對象,這再打印對象就調(diào)用對象自己的toString了,需要的朋友可以參考下
    2022-04-04
  • 解決SpringBoot下Redis序列化亂碼的問題

    解決SpringBoot下Redis序列化亂碼的問題

    這篇文章主要介紹了解決SpringBoot下Redis序列化亂碼的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot之@Aspect注解解讀

    SpringBoot之@Aspect注解解讀

    這篇文章主要介紹了SpringBoot之@Aspect注解解讀,AOP是面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù),而@Aspect 就是把一個類定義為切面供容器讀取,需要的朋友可以參考下
    2024-01-01
  • Java判斷字符串為空、字符串是否為數(shù)字

    Java判斷字符串為空、字符串是否為數(shù)字

    這篇文章主要介紹了Java判斷字符串為空、字符串是否為數(shù)字,其中數(shù)字的判斷介紹了3種方法,需要的朋友可以參考下
    2014-06-06
  • java中多線程的超詳細(xì)介紹

    java中多線程的超詳細(xì)介紹

    這篇文章主要給大家介紹了關(guān)于java中多線程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論