首頁  >  文章  >  Java  >  java雙向鍊錶實作的範例程式碼分享

java雙向鍊錶實作的範例程式碼分享

黄舟
黄舟原創
2017-03-27 10:16:561501瀏覽

這篇文章主要介紹了java 實作雙向鍊錶實例詳解的相關資料,需要的朋友可以參考下

java 實現雙向鍊錶實例詳解

## 雙向鍊錶是一個基本的資料結構,在Java中LinkedList已經實作了這個結構,不過身為開發者,也要擁有自己顯示這種結構的能力。話不多說,上碼:


    先是鍊錶的節點類別:

/** 
 * 链表节点 
 * @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">/** 
 * 链表实现类 
 * @author Administrator 
 * 
 * @param <T> 
 */ 
public class Chain<T> { 
 //头节点 
 private ChainNode<T> headNode; 
 //末尾节点指针 
 private ChainNode<T> lastNode; 
 private int size; 
  
  
 //创建链表就创建头节点 
 public Chain() { 
  this.headNode = new ChainNode<T>(null); 
  this.lastNode = headNode; 
   
 } 
 //增加一个节点 
 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++; 
  } 
 } 
 //删除指定索引的节点 
 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--; 
    //重新设置节点的编号 
    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"); 
 } 
 //获取指定索引的节点 
 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"); 
 } 
 //修改对应索引的节点 
 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"); 
 } 
 //是否包含对应数据的节点 
 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; 
 } 
 //获取节点数量(不含头节点) 
 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(); 
  } 
 } 
}

結果:

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

以上是java雙向鍊錶實作的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn