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

java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例

 更新時(shí)間:2014年03月21日 09:14:14   作者:  
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表的示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

/**
 * 雙向鏈表的實(shí)現(xiàn)
 * @author Skip
 * @version 1.0
 */
public class DoubleNodeList<T> {
 //節(jié)點(diǎn)類
 private static class Node<T>{
  Node<T> perv;  //前節(jié)點(diǎn)
  Node<T> next;  //后節(jié)點(diǎn)
  T data;    //數(shù)據(jù)

  public Node(T t){
   this.data = t;
  }
 }
 private Node<T> head;  //頭節(jié)點(diǎn)
 private Node<T> last;  //尾節(jié)點(diǎn)
 private Node<T> other;  //備用節(jié)點(diǎn)存放臨時(shí)操作
 private int length;  //鏈表長度

 /**
  * 無參構(gòu)造
  */
 public DoubleNodeList(){
  head = new Node<T>(null);
  last = head;
  length = 0;
 }

 /**
  * 初始化時(shí)創(chuàng)建一個(gè)節(jié)點(diǎn)
  * @param data 數(shù)據(jù)
  */
 public DoubleNodeList(T data){
  head = new Node<T>(data);
  last = head;
  length = 1;
 }

 /**
  * 添加一個(gè)節(jié)點(diǎn)
  * @param data 添加的數(shù)據(jù)
  */
 public void add(T data){
  if(isEmpty()){
   head = new Node<T>(data);
   last = head;
   length++;
  }else{
   //尾插法
   other = new Node<T>(data);
   other.perv = last;
   last.next = other;
   last = other;
   length++;
  }
 }

 /**
  * 在指定數(shù)據(jù)后插入一個(gè)節(jié)點(diǎn)
  * @param data 指定的數(shù)據(jù)
  * @param insertData 插入的數(shù)據(jù)
  * @return 插入成功返回true,不成功返回false
  */
 public boolean addAfert(T data , T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node<T> t = new Node<T>(insertData);
    t.perv = other;
    t.next = other.next;
    other.next = t;
    //判斷是否在最后一個(gè)節(jié)點(diǎn)后添加節(jié)點(diǎn)
    if(t.next==null){
     last = t;
    }
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定數(shù)據(jù)前插入一個(gè)節(jié)點(diǎn)
  * @param data 指定的數(shù)據(jù)
  * @param insertData 插入的數(shù)據(jù)
  * @return 插入成功返回true,不成功返回false
  */
 public boolean addBefore(T data, T insertData){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    Node<T> t = new Node<T>(insertData);
    t.perv = other.perv;
    t.next = other;
    other.perv.next = t;
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 獲得索引處的數(shù)據(jù)
  * @param index 索引
  * @return 數(shù)據(jù)
  */
 public T get(int index){
  if(index>length || index<0){
   throw new IndexOutOfBoundsException("索引越界:"+index);
  }
  other = head;
  for(int i=0;i<index;i++){
   other = other.next;
  }
  return other.data;
 }

 /**
  * 新值替換舊值
  * @return 成功為true,未找到為false
  */
 public boolean set(T oldValue,T newValue){
  other = head;
  while(other!=null){
   if(other.data.equals(oldValue)){
    other.data = newValue;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 移除指定的元素
  * @param data 需要移除的元素
  * @return 不存在為false,成功為true
  */
 public boolean remove(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    other.perv.next = other.next;
    length--;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 鏈表中是否包含此元素
  * @return 包含為true,不包含為false
  */
 public boolean contains(T data){
  other = head;
  while(other != null){
   if(other.data.equals(data)){
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 獲得最后一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  * @return 最后一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  */
 public T getLast(){
  return last.data;
 }

 /**
  * 獲得第一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  * @return 第一個(gè)節(jié)點(diǎn)的數(shù)據(jù)
  */
 public T getFirst(){
  return head.data;
 }

 /**
  * 獲得鏈表的長度
  * @return 長度
  */
 public int getSize(){
  return length;
 }

 /**
  * 是否為空鏈表
  * @return 空鏈表為true,非空鏈表為false
  */
 public boolean isEmpty(){
  return length==0;
 }

 /**
  * 清空鏈表
  */
 public void clear(){
  head = null;
  length = 0;
 }

 /**
  * 輸出鏈表內(nèi)所有節(jié)點(diǎn)
  */
 public void printList(){
  if(isEmpty()){
   System.out.println("空鏈表");
  }else{
   other = head;
   for(int i=0;i<length;i++){
    System.out.print(other.data+" ");
    other = other.next;
   }
   System.out.println();
  }
 }
}

相關(guān)文章

  • Java分代垃圾回收策略原理詳解

    Java分代垃圾回收策略原理詳解

    這篇文章主要介紹了Java分代垃圾回收策略原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java利用策略模式優(yōu)化過多if else代碼

    Java利用策略模式優(yōu)化過多if else代碼

    這篇文章主要介紹了Java利用策略模式優(yōu)化過多if else代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot接口調(diào)用之后報(bào)404問題的解決方案

    SpringBoot接口調(diào)用之后報(bào)404問題的解決方案

    這篇文章主要介紹了SpringBoot接口調(diào)用之后報(bào)404問題的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。
    2021-06-06
  • Java中equals()方法重寫實(shí)現(xiàn)代碼

    Java中equals()方法重寫實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java中equals()方法重寫實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • MyBatisPlus中@TableField注解的基本使用

    MyBatisPlus中@TableField注解的基本使用

    這篇文章主要介紹了MyBatisPlus中@TableField注解的基本使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java面向?qū)ο笾畣卫O(shè)計(jì)模式詳解

    Java面向?qū)ο笾畣卫O(shè)計(jì)模式詳解

    這篇文章主要介紹了Java面向?qū)ο笾畣卫O(shè)計(jì)模式詳解,所謂類的單例設(shè)計(jì)模式,就是采取一定的方法保證在整個(gè)的軟件系統(tǒng)中,對某個(gè)類只能存在一個(gè)對象實(shí)例,并且該類只提供一個(gè)取得其對象實(shí)例的方法,需要的朋友可以參考下
    2024-01-01
  • 基于編譯虛擬機(jī)jvm—openjdk的編譯詳解

    基于編譯虛擬機(jī)jvm—openjdk的編譯詳解

    下面小編就為大家分享一篇基于編譯虛擬機(jī)jvm—openjdk的編譯詳解,具有很好的參考價(jià)值,希望對大家有所幫助
    2017-12-12
  • 解決java項(xiàng)目jar打包后讀取文件失敗的問題

    解決java項(xiàng)目jar打包后讀取文件失敗的問題

    這篇文章主要介紹了解決java項(xiàng)目jar打包后讀取文件失敗的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot多數(shù)據(jù)源切換實(shí)現(xiàn)代碼(Mybaitis)

    SpringBoot多數(shù)據(jù)源切換實(shí)現(xiàn)代碼(Mybaitis)

    實(shí)際工作中我們會(huì)遇到springboot項(xiàng)目初始化啟動(dòng)時(shí)候,不能指定具體連接哪個(gè)數(shù)據(jù)源的時(shí)候,不同的接口連接不同的數(shù)據(jù)源或者前端頁面指定連接某個(gè)數(shù)據(jù)源等等情況,就會(huì)遇到動(dòng)態(tài)數(shù)據(jù)源切換的問題,需要的朋友可以參考下
    2022-04-04
  • 徹底理解Spring注解@Autowired實(shí)現(xiàn)原理

    徹底理解Spring注解@Autowired實(shí)現(xiàn)原理

    這篇文章主要為大家詳細(xì)的介紹了Spring注解@Autowired實(shí)現(xiàn)的原理,縝密的邏輯分析,實(shí)踐應(yīng)用示例操作說明,讓大家徹底的理解Spring注解@Autowired背后實(shí)現(xiàn)原理
    2022-03-03

最新評論