JAVA data structure linked list operates on a circular linked list

  • 2020-05-10 18:19:38
  • OfStack

JAVA linked list operation: circular linked list

Main analysis examples:

1. Single linked list circulates linked list

2. Double list circulates linked list

Single list node and double list node class and interface ICommOperate < T > I will not go into details here, as in part 1 above. Reference: JAVA list operation: singly linked lists and double linked list / / www ofstack. com article / 95113. htm

1. Single linked list circulates linked list


 
package LinkListTest;

import java.util.HashMap;
import java.util.Map;

public class SingleCycleLinkList implements ICommOperate<SNode> {
  private SNode head = new SNode("HEAD") ; //  Common header pointer, unchanged after declaration 
  private int size = 0 ;
  public int getSize() {
    return this.size;
  }
  
  /*
   *  List inserts, each time to the end , The criterion for judging the terminal is next Whether or not to head
   * */
  @Override
  public boolean insertNode(SNode node) {
    boolean flag = false ; 
    
    initLinkList() ; //  Initializes the linked list 
    if( this.size==0 ){ //  An empty list 
      this.head.setNextNode(node) ;
      node.setNextNode(this.head) ;
    }else{
      SNode current = this.head ;
      while( current.getNextNode()!=this.head ){ //  Find the end node 
        current = current.getNextNode() ;
      }
      current.setNextNode(node) ;
      node.setNextNode(this.head) ; //  Follow the linked list and point to the tail node head
    }
    this.size++ ;
    flag = true ;
    
    return flag;
  }
  
  /*
   *  Insert the list to specify the location pos, from 1 start , while pos Is greater than size Insert the end of the list 
   * */
  @Override
  public boolean insertPosNode(int pos, SNode node) {
    boolean flag = true ; 
    SNode current = this.head.getNextNode() ;
    
    initLinkList() ;//  Initializes the linked list 
    if( this.size==0 ){         //  The list is empty 
      this.head.setNextNode(node) ;
      node.setNextNode(this.head) ;//  Follow the linked list and point to the tail node head
      this.size++ ;
    }else if( this.size<pos ){      // pos Position greater than list length, insert end 
      insertNode(node) ;
    }else if( pos>0 && pos<=this.size ){ //  List of nodes 
      // 1 , find to insert pos Position node and front node, node It inserts between two nodes 
      int find = 0;
      SNode preNode = this.head; //  Before the node 
      SNode currentNode = current; //  The current node 
      while( find<pos-1 && currentNode!=this.head ){
        preNode = current ;             //  The front node moves back 
        currentNode = currentNode.getNextNode() ; //  The current node moves backward 
        find++ ;
        if( find<pos-1 && currentNode!=this.head ){ //  Before the end of the search node, move back to the front node 
          current = current.getNextNode() ;
        }
      }
//      System.out.println(preNode);
//      System.out.println(currentNode);
      
      // 2 , insert node 
      preNode.setNextNode(node);
      node.setNextNode(currentNode);
      this.size++ ;
    }else {
      System.out.println(" Location error ");
      flag = false ;
    }
    
    return flag;
  }

  private void initLinkList(){
    if( size==0 ){
      this.head.setNextNode(this.head);
    }
  }
  
  /*
   *  Specifies the nodes of the linked list pos , delete the corresponding node. How to: find the before and after nodes to delete , To delete , The subscript from 1 start 
   * */
  @Override
  public boolean deleteNode(int pos) {
    boolean flag = false; 
    SNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.size || current==this.head ){
      System.out.println(" Location information error or no information in linked list ");
    }else{
      // 1 , find the before and after nodes to delete 
      int find = 0;
      SNode preNode = this.head; //  Before the node 
      SNode nextNode = current.getNextNode(); //  After the node 
      while( find<pos-1 && nextNode!=this.head ){
        preNode = current ;          //  The front node moves back 
        nextNode = nextNode.getNextNode() ; //  The back node moves back 
        find++ ;
        if( find<pos-1 && nextNode!=this.head ){ //  Before the node is found, move back " Before the node "
          current = current.getNextNode() ;
        }
      }
//      System.out.println(preNode);
//      System.out.println(nextNode);
      
      // 2 , delete the node 
      preNode.setNextNode(nextNode);
      System.gc(); //  Reclaim delete node 
      this.size-- ;
      flag = true ;
    }
    
    return flag;
  }
  
  /*
   *  Specifies the nodes of the linked list pos , modify the corresponding node , The subscript from 1 start 
   * */
  @Override
  public boolean updateNode(int pos, Map<String, Object> map) {
    boolean flag = false ;
    SNode node = getNode(pos, map); //  Get the position pos The nodes of the 
    if( node!=null ){
      String data = (String) map.get("data") ;
      node.setData(data);
      flag = true ;
    }
    return flag;
  }
  
  /*
   *  Finds the node of the specified list pos, The subscript from 1 start 
   * */
  @Override
  public SNode getNode(int pos, Map<String, Object> map) {
    SNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.size || current==this.head ){
      System.out.println(" Location error or linked list does not exist ");
      return null;
    }
    int find = 0 ;
    while( find<pos-1 && current!=this.head ){
      current = current.getNextNode() ;
      find++ ;
    }
    return current;
  }

  /*
   *  Print the list 
   * */
  @Override
  public void printLink() {
    int length = this.size ;
    if( length==0 ){
      System.out.println(" The list is empty! ");
      return ;
    }
    SNode current = this.head.getNextNode() ;
    System.out.println(" Total number of points : " + length +"  a ");
    int find = 0 ;
    while( current!=this.head ){
      System.out.println(" The first  " + (++find) + "  A node   : " + current);
      current=current.getNextNode() ;
    }
  }
  
  public static void main(String[] args) {
    SingleCycleLinkList scll = new SingleCycleLinkList() ;
    SNode node1 = new SNode(" node 1");
    SNode node2 = new SNode(" node 2");
    SNode node3 = new SNode(" node 3");
    SNode node4 = new SNode(" node 4");
    SNode node5 = new SNode(" node 5");
    SNode node6 = new SNode(" Insert specified position ");
//    scll.insertPosNode(scll.getSize()+1, node1) ;
//    scll.insertPosNode(scll.getSize()+1, node2) ;
//    scll.insertPosNode(scll.getSize()+1, node3) ;
//    scll.insertPosNode(scll.getSize()+1, node4) ;
//    scll.insertPosNode(scll.getSize()+1, node5) ;
    scll.insertNode(node1);
    scll.insertNode(node2);
    scll.insertNode(node3);
    scll.insertNode(node4);
    scll.insertNode(node5);
    
    System.out.println("******************* The output list *******************");
    scll.printLink();
    
    System.out.println("******************* Gets the specified list node *******************");
    int pos = 2 ;
    System.out.println(" Gets list number  "+pos+"  Position data   : "+scll.getNode(pos, null));
    
    System.out.println("******************* Inserts a node into the list at the specified location *******************");
    int pos1 = 3 ;
    System.out.println(" Insert the data into the data "+pos1+" A node: ");
    scll.insertPosNode(pos1, node6) ;
    scll.printLink();
    
    System.out.println("******************* Deletes a linked list to specify a location node *******************");
    int pos2 = 3 ;
    System.out.println(" Delete the first "+pos2+" A node: ");
    scll.deleteNode(pos2) ;
    scll.printLink();
    
    System.out.println("******************* Modify the list to specify the location node *******************");
    int pos3 = 3 ;
    System.out.println(" Modify the first "+pos3+" A node: ");
    Map<String, Object> map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    scll.updateNode(pos3, map) ;
    scll.printLink();
  }

}

  2. Double list circular linked list




package LinkListTest;

import java.util.HashMap;
import java.util.Map;

public class DoubleCycleLinkList implements ICommOperate<DNode>{
  private DNode head = new DNode("HEAD"); //  Common header pointer, unchanged after declaration 
  private int size = 0 ; //  Record the number of linked list nodes 
  
  public int getSize() {
    return this.size;
  }
  
  /*
   *  List inserts, each time to the end , The criterion for judging the terminal is next Whether or not to head
   * */
  @Override
  public boolean insertNode(DNode node) {
    boolean flag = false ; 
    
    initLinkList() ; //  Initializes the linked list 
    DNode current = this.head ;
    if( this.size==0 ){  //  An empty list 
      this.head.setNextNode(node) ;
      node.setPriorNode(this.head);
      node.setNextNode(this.head) ;
    }else{        //  List of nodes 
      while( current.getNextNode()!=this.head ){ //  Find the end node 
        current = current.getNextNode() ;
      }
      current.setNextNode(node) ;
      node.setPriorNode(current);
      node.setNextNode(this.head) ; //  Follow the linked list and point to the tail node head
    }
    this.size++ ;
    flag = true ;
    
    return flag;
  }
  
  /*
   *  Insert the list to specify the location pos, from 1 start , while pos Is greater than size Insert the end of the list 
   * */
  @Override
  public boolean insertPosNode(int pos, DNode node) {
    boolean flag = true; 
    
    initLinkList() ; //  Initializes the linked list 
    DNode current = this.head.getNextNode() ;
    if( this.size==0 ){           //  The list is empty 
      this.head.setNextNode(node) ;
      node.setPriorNode(this.head);
      node.setNextNode(this.head) ;
      this.size++ ;
    }else if( pos>this.size ){         // pos Position greater than list length, insert end 
      insertNode(node) ;
    }else if( pos>0 && pos<=this.size ){  //  List of nodes 
      // 1 , find the insertion position pos node , insert pos Current location of node 
      int find = 0;
      while( find<pos-1 && current.getNextNode()!=this.head ){
        current = current.getNextNode() ;
        find++ ;
      }
      // 2 , insert node 
      if( current.getNextNode()==this.head ){ //  End node 
        node.setPriorNode(current);
        node.setNextNode(this.head);
        current.setNextNode(node);
      } else if( current.getNextNode()!=this.head ) { // Intermediate nodes 
        node.setPriorNode(current.getPriorNode());
        node.setNextNode(current);
        current.getPriorNode().setNextNode(node);
        current.setPriorNode(node);
      } 
      this.size++ ;
    }else{
      System.out.println(" Location error ");
      flag = false ;
    }
    return flag;
  }

  private void initLinkList(){
    if( size==0 ){
      this.head.setNextNode(this.head);
      this.head.setPriorNode(this.head);
    }
  }
  
  /*
   *  Specifies the nodes of the linked list pos , delete the corresponding node. How to: find the before and after nodes to delete , The subscript from 1 start 
   * */
  @Override
  public boolean deleteNode(int pos) {
    boolean flag = false; 
    DNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.size || current==this.head ){
      System.out.println(" Location error or linked list does not exist ");
    }else{
      // 1 , find the location to delete pos node 
      int find = 0;
      while( find<pos-1 && current.getNextNode()!=this.head ){
        current = current.getNextNode() ;
        find++ ;
      }
      // 2 , delete the node 
      if( current.getNextNode()==this.head ){ //  End node 
        current.getPriorNode().setNextNode(this.head) ;
      } else if( current.getNextNode()!=this.head ) { // Intermediate nodes 
        current.getPriorNode().setNextNode(current.getNextNode()) ;
        current.getNextNode().setPriorNode(current.getPriorNode()) ;
      } 
      System.gc(); //  Reclaim delete node 
      this.size-- ;
      flag = true ;
    }
    return flag;
  }
  
  /*
   *  Specifies the nodes of the linked list pos , modify the corresponding node , The subscript from 1 start 
   * */
  @Override
  public boolean updateNode(int pos, Map<String, Object> map) {
    boolean flag = false ;
    DNode node = getNode(pos, map);
    if( node!=null ){
      String data = (String) map.get("data") ;
      node.setData(data);
      flag = true ;
    }
    return flag;
  }

  /*
   *  Finds the node of the specified list pos, The subscript from 1 start 
   * */
  @Override
  public DNode getNode(int pos, Map<String, Object> map) {
    DNode current = this.head.getNextNode() ;
    if( pos<=0 || pos>this.size || current==this.head ){
      System.out.println(" Location error or linked list does not exist ");
      return null;
    }
    int find = 0 ;
    while( find<pos-1 && current!=this.head ){
      current = current.getNextNode() ;
      find++ ;
    }
    return current;
  }

  /*
   *  Print the list 
   * */
  @Override
  public void printLink() {
    int length = this.size ;
    if( length==0 ){
      System.out.println(" The list is empty! ");
      return ;
    }
    DNode current = this.head.getNextNode() ;
    int find = 0 ; 
    System.out.println(" Total number of points : " + length +"  a ");
    while( current!=this.head ){
      System.out.println(" The first  " + (++find) + "  A node   : " + current);
      current=current.getNextNode() ;
    }
  }
  
  public static void main(String[] args) {
    DoubleCycleLinkList dcll = new DoubleCycleLinkList() ;
    DNode node1 = new DNode(" node 1");
    DNode node2 = new DNode(" node 2");
    DNode node3 = new DNode(" node 3");
    DNode node4 = new DNode(" node 4");
    DNode node5 = new DNode(" node 5");
    DNode node6 = new DNode(" Insert specified position ");
    dcll.insertPosNode(10, node1) ;
    dcll.insertPosNode(10, node2) ;
    dcll.insertPosNode(8, node3) ;
    dcll.insertPosNode(88, node4) ;
    dcll.insertPosNode(8, node5) ;
//    dcll.insertNode(node1);
//    dcll.insertNode(node2);
//    dcll.insertNode(node3);
//    dcll.insertNode(node4);
//    dcll.insertNode(node5);
    
    System.out.println("******************* The output list *******************");
    dcll.printLink();
    
    System.out.println("******************* Gets the specified list node *******************");
    int pos = 2 ;
    System.out.println(" Gets list number  "+pos+" Position data   : "+dcll.getNode(pos, null));
    
    System.out.println("******************* Inserts a node into the list at the specified location *******************");
    int pos1 = dcll.getSize()+1 ;
    System.out.println(" Insert the data into the data "+pos1+" A node: ");
    dcll.insertPosNode(pos1, node6) ;
    dcll.printLink();
    
    System.out.println("******************* Deletes a linked list to specify a location node *******************");
    int pos2 = 7 ;
    System.out.println(" Delete the first "+pos2+" A node: ");
    dcll.deleteNode(pos2) ;
    dcll.printLink();
    
    System.out.println("******************* Modify the list to specify the location node *******************");
    int pos3 = 3 ;
    System.out.println(" Modify the first "+pos3+" A node: ");
    Map<String, Object> map = new HashMap<>() ;
    map.put("data", "this is a test") ;
    dcll.updateNode(pos3, map) ;
    dcll.printLink();
  }
}



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


Related articles: