JAVA linked list operation: circular link list
Main analysis examples:
1. Single linked list recurrent list
2. Double-linked list recurrent list
Among them, the single-linked list node and double-linked list node class and interface ICommOperate<T> are consistent with the previous article, and will not be discussed here. Reference: JAVA linked list operations: single-linked list and double-linked list //www.VeVB.COM/article/95113.htm
1. Single linked list recurrent list
package LinkListTest;import java.util.HashMap;import java.util.Map;public class SingleCycleLinkList implements ICommOperate<SNode> { private SNode head = new SNode("HEAD"); // public header pointer, unchanged after declaration private int size = 0 ; public int getSize() { return this.size; } /* * Insert the linked list, each time it is inserted to the end, the standard for determining whether the end is next pointing to the head * */ @Override public boolean insertNode(SNode node) { boolean flag = false ; initLinkList() ; // Initialize the linked list if( this.size==0 ){ // Empty linked 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 bad linked list, the tail node points to the head } this.size++ ; flag = true ; return flag; } /* * Insert the specified position of the linked list, starting from 1, and pos is greater than size, insert it at the end of the linked list * */ @Override public boolean insertPosNode(int pos, SNode node) { boolean flag = true ; SNode current = this.head.getNextNode() ; initLinkList() ;// Initialize the linked list if( this.size==0 ){ // The linked list is empty this.head.setNextNode(node); node.setNextNode(this.head) ;// Follow the bad linked list, the tail node points to head this.size++ ; }else if( this.size<pos ){ // The pos position is greater than the length of the linked list, insert the end insertNode(node); }else if( pos>0 && pos<=this.size ){ // The node in the linked list// 1. Find the node and the previous node to be inserted, and the node will be inserted between the two nodes int find = 0; SNode preNode = this.head; // The front node SNode currentNode = current; // The current node while( find<pos-1 && currentNode!=this.head ){ preNode = current ; // The front node moves backwards currentNode = currentNode.getNextNode() ; // The current node is moved backward find++ ; if( find<pos-1 && currentNode!=this.head ){ // The node is not finished looking for the node, and the node is moved backward 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("Position information error"); flag = false ; } return flag; } private void initLinkList(){ if( size==0 ){ this.head.setNextNode(this.head); } } /* * Specify the node pos of the linked list and delete the corresponding node. Method: Find the front and back nodes to delete, delete, and the subscript starts from 1* */ @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("Position information error or no information in the linked list"); }else{ // 1. Find the front and back nodes to delete int find = 0; SNode preNode = this.head; // Front node SNode nextNode = current.getNextNode(); // The back node while( find<pos-1 && nextNode!=this.head ){ preNode = current ; // The front node is moved back nextNode = nextNode.getNextNode() ; // The back node is moved back find++ ; if( find<pos-1 && nextNode!=this.head ){ // The front node is not finished, the back "front node" is moved back current = current.getNextNode() ; } }// System.out.println(preNode);// System.out.println(nextNode); // 2. Delete the node preNode.setNextNode(nextNode); System.gc(); // Recycle and delete the node this.size-- ; flag = true ; } return flag; } /* * Specify the node pos of the linked list, modify the corresponding node, and the subscript starts from 1* */ @Override public boolean updateNode(int pos, Map<String, Object> map) { boolean flag = false ; SNode node = getNode(pos, map); // Get the node at the corresponding pos if( node!=null ){ String data = (String) map.get("data"); node.setData(data); flag = true ; } return flag; } /* * Find the node pos of the specified linked list, and the subscript starts from 1* */ @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("Position information is incorrect or the linked list does not exist"); return null; } int find = 0 ; while( find<pos-1 && current!=this.head ){ current = current.getNextNode() ; find++ ; } return current; } /* * Print linked list* */ @Override public void printLink() { int length = this.size ; if( length==0 ){ System.out.println("The linked list is empty! "); return ; } SNode current = this.head.getNextNode() ; System.out.println("Total number of nodes: " + length +" ones"); int find = 0 ; while( current!=this.head ){ System.out.println("th" + (++find) + "nodes: " + current); current=current.getNextNode() ; } } public static void main(String[] args) { SingleCycleLinkList scll = new SingleCycleLinkList() ; SNode node1 = new SNode("Node1"); SNode node2 = new SNode("Node2"); SNode node3 = new SNode("Node3"); SNode node4 = new SNode("Node4"); SNode node5 = new SNode("Node5"); SNode node6 = new SNode("Insert specified position");// scll.insertPosNode(scll.getSize()+1, node1);// scll.insertPosNode(scll.getSize()+1, node2);// 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("*********************************"); scll.printLink(); System.out.println("************************ Get the specified linked list node*************************************"); int pos = 2; System.out.println("get the "+pos+" position data of the linked list: "+scll.getNode(pos, null)); System.out.println("*********************************"); int pos1 = 3 ; System.out.println("Insert data into the "+pos1+" nodes:"); scll.insertPosNode(pos1, node6); scll.printLink(); System.out.println("***************************Delete the node specified in the linked list**********************************"); int pos2 = 3 ; System.out.println("Delete"+pos2+" nodes: "); scll.deleteNode(pos2); scll.printLink(); System.out.println("*********************************Modify the specified location nodes of the linked list*************************************"); int pos3 = 3; System.out.println("Modify"+pos3+" nodes: "); Map<String, Object> map = new HashMap<>() ; map.put("data", "this is a test"); scll.updateNode(pos3, map) ; scll.printLink(); }}2. Double-linked list recurrent list
package LinkListTest;import java.util.HashMap;import java.util.Map;public class DoubleCycleLinkList implements ICommOperate<DNode>{ private DNode head = new DNode("HEAD"); // Public header pointer, unchanged after declaration private int size = 0; // Record the number of nodes of linked list public int getSize() { return this.size; } /* * Insert the linked list, each time it is inserted to the end, the standard for determining whether the end is next pointing to the head * */ @Override public boolean insertNode(DNode node) { boolean flag = false ; initLinkList() ; // Initialize the linked list DNode current = this.head ; if( this.size==0 ){ // The empty linked list this.head.setNextNode(node) ; node.setPriorNode(this.head); node.setNextNode(this.head); }else{ // The node in the linked list while( current.getNextNode()!=this.head ){ // Find the end node current = current.getNextNode() ; } current.setNextNode(node); node.setPriorNode(current); node.setNextNode(this.head); // Redirect the bad linked list, the tail node points to the head } this.size++ ; flag = true ; return flag; } /* * Insert the specified position of the linked list, starting from 1, and pos is greater than size, insert the end of the linked list* */ @Override public boolean insertPosNode(int pos, DNode node) { boolean flag = true; initLinkList() ; // Initialize the linked list DNode currently = this.head.getNextNode() ; if( this.size==0 ){ // The linked list is empty this.head.setNextNode(node); node.setPriorNode(this.head); node.setNextNode(this.head); this.size++; }else if( pos>this.size ){ // The pos position is greater than the length of the linked list, insert the end insertNode(node); }else if( pos>0 && pos<=this.size ){ // The node in the linked list// 1. Find the pos node to be inserted, insert the pos node current position int find = 0; while( find<pos-1 && current.getNextNode()!=this.head){ current = current.getNextNode() ; find++ ; } // 2. Insert node if( current.getNextNode()==this.head ){ // tail node node.setPriorNode(current); node.setNextNode(this.head); current.setNextNode(node); } else if( current.getNextNode()!=this.head ) { // intermediate node node.setPriorNode(current.getPriorNode()); node.setNextNode(current); current.getPriorNode().setNextNode(node); current.setPriorNode(node); } this.size++ ; }else{ System.out.println("Position Information Error"); flag = false ; } return flag; } private void initLinkList(){ if( size==0 ){ this.head.setNextNode(this.head); this.head.setPriorNode(this.head); } } /* * Specify the node pos of the linked list and delete the corresponding node. Method: Find the front and back node deletion of the node to be deleted, and the subscript starts from 1* */ @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("Position information is incorrect or the linked list does not exist"); }else{ // 1. Find the location pos node to be deleted int find = 0; while( find<pos-1 && current.getNextNode()!=this.head ){ current = current.getNextNode() ; find++ ; } // 2. Delete node if( current.getNextNode()==this.head ){ // tail node current.getPriorNode().setNextNode(this.head) ; } else if( current.getNextNode()!=this.head ) { // intermediate node current.getPriorNode().setNextNode(current.getNextNode()) ; current.getNextNode().setPriorNode(current.getPriorNode()) ; } System.gc(); // Recycle and delete nodes this.size-- ; flag = true ; } return flag; } /* * Specify the node pos of the linked list, modify the corresponding node, and the subscript starts from 1* */ @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; } /* * Find the node pos of the specified linked list, and the subscript starts from 1* */ @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("Position information is incorrect or the linked list does not exist"); return null; } int find = 0 ; while( find<pos-1 && current!=this.head ){ current = current.getNextNode() ; find++ ; } return current; } /* * Print linked list* */ @Override public void printLink() { int length = this.size ; if( length==0 ){ System.out.println("The linked list is empty! "); return ; } DNode current = this.head.getNextNode() ; int find = 0 ; System.out.println("Total number of nodes: " + length +" ones"); while( current!=this.head ){ System.out.println("th" + (++find) + "one nodes: " + current); current=current.getNextNode() ; } } public static void main(String[] args) { DoubleCycleLinkList dcll = new DoubleCycleLinkList() ; DNode node1 = new DNode("Node1"); DNode node2 = new DNode("Node2"); DNode node3 = new DNode("Node3"); DNode node4 = new DNode("Node4"); DNode node5 = new DNode("Node5"); 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("****************************** Output linked list*******************************"); dcll.printLink(); System.out.println("************************************"); int pos = 2 ; System.out.println("Get the "+pos+" position data of the linked list: "+dcll.getNode(pos, null)); System.out.println("****************************** Insert node into the specified position of the linked list**********************************"); int pos1 = dcll.getSize()+1 ; System.out.println("Insert data into "+pos1+" nodes:"); dcll.insertPosNode(pos1, node6); dcll.printLink(); System.out.println("************************Delete the node specified location of the linked list**********************************"); int pos2 = 7; System.out.println("Delete"+pos2+" nodes:"); dcll.deleteNode(pos2); dcll.printLink(); System.out.println("***************************************"); int pos3 = 3 ; System.out.println("Modify the "+pos3+" nodes: "); 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 it can help you. Thank you for your support for this site!