亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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),不過作為開發(fā)者,也要擁有自己顯示這種結(jié)構(gòu)的能力。話不多說,上代碼:

    首先是鏈表的節(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)文章

  • spring Boot查詢數(shù)據(jù)分頁顯示的方法實(shí)例

    spring Boot查詢數(shù)據(jù)分頁顯示的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于spring Boot查詢數(shù)據(jù)分頁顯示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 詳解JVM 運(yùn)行時(shí)內(nèi)存使用情況監(jiān)控

    詳解JVM 運(yùn)行時(shí)內(nèi)存使用情況監(jiān)控

    這篇文章主要介紹了詳解JVM 運(yùn)行時(shí)內(nèi)存使用情況監(jiān)控,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Spring Boot整合Redis的完整步驟

    Spring Boot整合Redis的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot整合Redis的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java從零實(shí)現(xiàn)超市會(huì)員管理系統(tǒng)

    Java從零實(shí)現(xiàn)超市會(huì)員管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)超市會(huì)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-12-12
  • BigDecimal divide除法除不盡報(bào)錯(cuò)的問題及解決

    BigDecimal divide除法除不盡報(bào)錯(cuò)的問題及解決

    這篇文章主要介紹了BigDecimal divide除法除不盡報(bào)錯(cuò)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作

    Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作

    這篇文章主要介紹了Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • spring?boot中spring框架的版本升級(jí)圖文教程

    spring?boot中spring框架的版本升級(jí)圖文教程

    Spring Boot是一款基于Spring框架的快速開發(fā)框架,它提供了一系列的開箱即用的功能和組件,這篇文章主要給大家介紹了關(guān)于spring?boot中spring框架的版本升級(jí)的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • 使用springboot+druid雙數(shù)據(jù)源動(dòng)態(tài)配置操作

    使用springboot+druid雙數(shù)據(jù)源動(dòng)態(tài)配置操作

    這篇文章主要介紹了使用springboot+druid雙數(shù)據(jù)源動(dòng)態(tài)配置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java中封裝的實(shí)現(xiàn)方法詳解

    java中封裝的實(shí)現(xiàn)方法詳解

    在本篇文章里我們給大家詳細(xì)分享了關(guān)于java中封裝的實(shí)現(xiàn)方法,有需要的朋友們跟著學(xué)習(xí)下。
    2018-10-10
  • 深入理解Mybatis二級(jí)緩存

    深入理解Mybatis二級(jí)緩存

    與一級(jí)緩存相比,二級(jí)緩存范圍更大了一些,可以被多個(gè)SqlSession所共用。下面通過本文帶領(lǐng)大家一起學(xué)習(xí)mybatis二級(jí)緩存知識(shí),一起看看吧
    2016-12-12

最新評(píng)論