java中的雙向鏈表是如何實(shí)現(xiàn)的

這篇文章將為大家詳細(xì)講解有關(guān)java 中的雙向鏈表是如何實(shí)現(xiàn)的,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)建站專注骨干網(wǎng)絡(luò)服務(wù)器租用十余年,服務(wù)更有保障!服務(wù)器租用,達(dá)州托管服務(wù)器 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。

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; 
 //對象編號 
 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)的編號 
    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"); 
 } 
 //修改對應(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"); 
 } 
 //是否包含對應(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; 
 } 
} 

測試一下:

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

關(guān)于java 中的雙向鏈表是如何實(shí)現(xiàn)的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:java中的雙向鏈表是如何實(shí)現(xiàn)的
本文路徑:http://muchs.cn/article18/jcjjgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、營銷型網(wǎng)站建設(shè)商城網(wǎng)站、網(wǎng)站內(nèi)鏈網(wǎng)站建設(shè)、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站