Implementation of java bidirectional linked list example details

  • 2020-06-15 09:01:28
  • OfStack

Implementation of java bidirectional linked list example details

A bi-directional linked list is a basic data structure that LinkedList already implements in Java, but you need to be able to display as a developer. Without further ado, the code above:

The first is the node class of the linked list:


/** 
 *  The linked list node  
 * @author Administrator 
 * 
 * @param <T> 
 */ 
public class ChainNode<T> { 
 private T data; 
 // Object code  
 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; 
 } 
  
  
} 

Then there's the linked list:


<pre name="code" class="java">/** 
 *  List implementation class  
 * @author Administrator 
 * 
 * @param <T> 
 */ 
public class Chain<T> { 
 // Head node  
 private ChainNode<T> headNode; 
 // Pointer to end node  
 private ChainNode<T> lastNode; 
 private int size; 
  
  
 // Creating a linked list creates a header node  
 public Chain() { 
  this.headNode = new ChainNode<T>(null); 
  this.lastNode = headNode; 
   
 } 
 // increase 1 A node  
 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++; 
  } 
 } 
 // Deletes the node of the specified index  
 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--; 
    // Reset the node number  
    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"); 
 } 
 // Gets the node of the specified index  
 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"); 
 } 
 // Modify the node of the corresponding index  
 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"); 
 } 
 // Whether the node contains the corresponding data  
 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; 
 } 
 // Get the number of nodes (without headers)  
 public int getSize() { 
  return size; 
 } 
} 

Test 1:


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(); 
  } 
 } 
} 

Results:


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

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: