最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解

 更新時(shí)間:2017年03月27日 08:43:17   投稿:lqh  
這篇文章主要介紹了java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

java 實(shí)現(xiàn)雙向鏈表實(shí)例詳解

 雙向鏈表是一個(gè)基本的數(shù)據(jù)結(jié)構(gòu),在Java中LinkedList已經(jīng)實(shí)現(xiàn)了這種結(jié)構(gòu),不過(guò)作為開(kāi)發(fā)者,也要擁有自己顯示這種結(jié)構(gòu)的能力。話不多說(shuō),上代碼:

    首先是鏈表的節(jié)點(diǎn)類:

/** 
 * 鏈表節(jié)點(diǎn) 
 * @author Administrator 
 * 
 * @param <T> 
 */ 
public class ChainNode<T> { 
 private T data; 
 //對(duì)象編號(hào) 
 private int dataNo; 
  
 public ChainNode<T> nextChainNode; 
 public ChainNode<T> preChainNode; 
 public ChainNode(T data, ChainNode<T> nextChainNode, 
   ChainNode<T> preChainNode) { 
  this.data = data; 
  this.nextChainNode = nextChainNode; 
  this.preChainNode = preChainNode; 
 } 
  
  
  
 public ChainNode(T data) { 
  this.data = data; 
 } 
 
  
 
 public int getDataNo() { 
  return dataNo; 
 } 
 
 
 
 public void setDataNo(int dataNo) { 
  this.dataNo = dataNo; 
 } 
 
 
 public void setData(T data) { 
  this.data = data; 
 } 
 
 
 
 public T getData() { 
  return data; 
 } 
  
  
} 

 然后是鏈表:

<pre name="code" class="java">/** 
 * 鏈表實(shí)現(xiàn)類 
 * @author Administrator 
 * 
 * @param <T> 
 */ 
public class Chain<T> { 
 //頭節(jié)點(diǎn) 
 private ChainNode<T> headNode; 
 //末尾節(jié)點(diǎn)指針 
 private ChainNode<T> lastNode; 
 private int size; 
  
  
 //創(chuàng)建鏈表就創(chuàng)建頭節(jié)點(diǎn) 
 public Chain() { 
  this.headNode = new ChainNode<T>(null); 
  this.lastNode = headNode; 
   
 } 
 //增加一個(gè)節(jié)點(diǎn) 
 public void addNode(T data) { 
  ChainNode<T> node = new ChainNode<T>(data); 
  if(lastNode != null){ 
   lastNode.nextChainNode = node; 
   node.preChainNode = node; 
   node.setDataNo(size); 
   lastNode = node; 
   size++; 
  } 
 } 
 //刪除指定索引的節(jié)點(diǎn) 
 public void deleteNode(int dataNo) throws Exception { 
  if(getSize() == 0){ 
   throw new Exception("chain is empty"); 
  } 
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) { 
   if(node.getDataNo() == dataNo){ 
    node.preChainNode.nextChainNode = node.nextChainNode; 
    if(node.nextChainNode != null){ 
     node.nextChainNode.preChainNode = node.preChainNode; 
    } 
     
    node.nextChainNode = null; 
    node.preChainNode = null; 
    size--; 
    //重新設(shè)置節(jié)點(diǎn)的編號(hào) 
    for (ChainNode<T> chainNode = node.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) { 
     chainNode.setDataNo(chainNode.getDataNo()-1); 
    } 
    return; 
     
   } 
  } 
  throw new Exception("the corresponding data that can not be found"); 
 } 
 //獲取指定索引的節(jié)點(diǎn) 
 public T get(int dataNo) throws Exception { 
  if(getSize() == 0){ 
   throw new Exception("chain is empty"); 
  } 
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) { 
   if(node.getDataNo() == dataNo){ 
    return node.getData(); 
   } 
  } 
  throw new Exception("the corresponding data that can not be found"); 
 } 
 //修改對(duì)應(yīng)索引的節(jié)點(diǎn) 
 public void set(int dataNo,T data) throws Exception { 
  if(getSize() == 0){ 
   throw new Exception("chain is empty"); 
  } 
  for (ChainNode<T> node = headNode.nextChainNode;node != null;node = node.nextChainNode) { 
   if(node.getDataNo() == dataNo){ 
    node.setData(data); 
    return; 
   } 
  } 
  throw new Exception("the data that is to be modified can not be found"); 
 } 
 //是否包含對(duì)應(yīng)數(shù)據(jù)的節(jié)點(diǎn) 
 public boolean isContains(T data) throws Exception { 
  if(getSize() == 0){ 
   throw new Exception("chain is empty"); 
  } 
  for (ChainNode<T> chainNode = headNode.nextChainNode;chainNode != null;chainNode = chainNode.nextChainNode) { 
   if(chainNode.getData() == data){ 
    return true; 
   } 
  } 
   
  return false; 
 } 
 //獲取節(jié)點(diǎn)數(shù)量(不含頭節(jié)點(diǎn)) 
 public int getSize() { 
  return size; 
 } 
} 

測(cè)試一下:

public class ChainTest { 
 public static void main(String[] args) throws Exception{ 
  String oneString = "one"; 
  String twoString = "two"; 
  String threeString = "three"; 
  String fourString = "four"; 
  Chain<String> chain = new Chain<String>(); 
  chain.addNode(oneString); 
  chain.addNode(twoString); 
  chain.addNode(threeString); 
  chain.addNode(fourString); 
  for (int i = 0; i < chain.getSize(); i++) { 
   String string2 = chain.get(i); 
   System.out.println(string2); 
  } 
  try { 
   String string = chain.get(2); 
   System.out.println("the data of the second node is"+string); 
   System.out.println(chain.isContains(fourString)); 
   chain.set(1, "haha"); 
   System.out.println("modify chain"); 
   for (int i = 0; i < chain.getSize(); i++) { 
    String string2 = chain.get(i); 
    System.out.println(string2); 
   } 
    
   chain.deleteNode(3); 
   System.out.println("delete one node"); 
   for (int i = 0; i < chain.getSize(); i++) { 
    String string2 = chain.get(i); 
    System.out.println(string2); 
   } 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 } 
} 

結(jié)果:

one
two
three
four
the data of the second node isthree
true
modify chain
one
haha
three
four
delete one node
one
haha
three

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • java解析{{}}變量名以及文本內(nèi)容替換操作

    java解析{{}}變量名以及文本內(nèi)容替換操作

    這篇文章主要介紹了java解析{{}}變量名以及文本內(nèi)容替換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • java jpa查詢沒(méi)有id表的方法

    java jpa查詢沒(méi)有id表的方法

    本文主要介紹了java jpa查詢沒(méi)有id表的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • java 實(shí)現(xiàn)圖片圓角處理、背景透明化

    java 實(shí)現(xiàn)圖片圓角處理、背景透明化

    這篇文章主要介紹了java 實(shí)現(xiàn)圖片圓角處理、背景透明化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 關(guān)于fastjson的@JSONField注解的一些問(wèn)題(詳解)

    關(guān)于fastjson的@JSONField注解的一些問(wèn)題(詳解)

    下面小編就為大家?guī)?lái)一篇關(guān)于fastjson的@JSONField注解的一些問(wèn)題(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Java實(shí)現(xiàn)FIFO任務(wù)調(diào)度隊(duì)列策略

    Java實(shí)現(xiàn)FIFO任務(wù)調(diào)度隊(duì)列策略

    在工作中,很多高并發(fā)的場(chǎng)景中,我們會(huì)用到隊(duì)列來(lái)實(shí)現(xiàn)大量的任務(wù)請(qǐng)求。當(dāng)任務(wù)需要某些特殊資源的時(shí)候,我們還需要合理的分配資源,讓隊(duì)列中的任務(wù)高效且有序完成任務(wù)。本文將為大家介紹通過(guò)java實(shí)現(xiàn)FIFO任務(wù)調(diào)度,需要的可以參考一下
    2021-12-12
  • springboot簡(jiǎn)單接入websocket的操作方法

    springboot簡(jiǎn)單接入websocket的操作方法

    這篇文章主要介紹了springboot簡(jiǎn)單接入websocket的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • SpringBoot中@EnableAutoConfiguration注解源碼分析

    SpringBoot中@EnableAutoConfiguration注解源碼分析

    這篇文章主要介紹了SpringBoot中@EnableAutoConfiguration注解源碼分析,@EnableAutoConfiguration,主要是用于加載Starter目錄包之外的、需要Spring自動(dòng)生成Bean對(duì)象的、帶有@Configuration注解的類,需要的朋友可以參考下
    2023-08-08
  • springboot項(xiàng)目編寫(xiě)發(fā)送異常日志到企微工具包的操作方法

    springboot項(xiàng)目編寫(xiě)發(fā)送異常日志到企微工具包的操作方法

    本文介紹了Springboot項(xiàng)目如何編寫(xiě)發(fā)送異常日志到企業(yè)微信的工具包,內(nèi)容包括創(chuàng)建基礎(chǔ)Bean、配置類、pom依賴等步驟,并展示了如何通過(guò)nacos進(jìn)行配置,這為開(kāi)發(fā)者提供了一種有效的日志管理方案,方便快速定位和處理項(xiàng)目中的異常問(wèn)題,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟

    Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟

    這篇文章主要介紹了Spring下Filter過(guò)濾器配置全局異常處理的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解

    Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解

    這篇文章主要介紹了Spring擴(kuò)展點(diǎn)之BeanFactoryPostProcessor詳解,Spring的設(shè)計(jì)非常優(yōu)雅,有很多的擴(kuò)展點(diǎn)供我們對(duì)項(xiàng)目進(jìn)行擴(kuò)展,今天學(xué)習(xí)一下Spring其中擴(kuò)展點(diǎn)之一的BeanFactoryPostProcessor,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

湖州市| 宁化县| 利辛县| 静海县| 曲靖市| 阿拉尔市| 舟山市| 巨鹿县| 嘉黎县| 萝北县| 屏东市| 衡水市| 吉林市| 松阳县| 万年县| 上饶市| 高青县| 蛟河市| 大同市| 尼勒克县| 新田县| 岳阳县| 灵山县| 纳雍县| 安多县| 连城县| 饶河县| 太仓市| 商丘市| 鄂伦春自治旗| 名山县| 遵义市| 江都市| 石门县| 涞水县| 宁化县| 江达县| 丰城市| 兴宁市| 大港区| 闵行区|