JAVA implements the function of adding and deleting two-way linked lists, complete code
package linked;class LinkedTable{ }public class LinkedTableTest { //Construct a single linked list static Node node1 = new Node("name1"); static Node node2 = new Node("name2"); static Node node3 = new Node("name3"); static Node node4 = new Node("name4"); static Node node5 = new Node("name5"); public static void main(String[] args) { //Set pointer setPoint(); //Loop through System.out.println("*******Initial linked list**********"); out(node1,node5); System.out.println(); //Insert node behind node2 addNode(node2,node3); //Loop through System.out.println("*******Insert node2.5*********"); out(node1, node5); System.out.println(); //Delete node node2.setNextNode(node3); node3.setNextNodeF(node2); // Loop through System.out.println("*******Delete node2.5*******"); out(node1, node5); System.out.println(); } //Set pointer public static void setPoint() { //Set forward pointer node1.setNextNode(node2); node2.setNextNode(node3); node3.setNextNode(node4); node4.setNextNode(node5); //Set reverse pointer node5.setNextNodeF(node4); node4.setNextNodeF(node3); node4.setNextNodeF(node3); node3.setNextNodeF(node2); node2.setNextNodeF(node1); } //Loop through the single linked list public static void outLinked(Node startNode){ Node node= new Node(); node.setNextNode(startNode); do { node=node.getNextNode(); System.out.print(node.getName()+"----"); } while(node.getNextNode()!=null); } //Reversely loop through the single linked list public static void outLinkedF(Node endNode){ Node node= new Node(); node.setNextNodeF(endNode); do { node=node.getNextNodeF(); System.out.print(node.getName()+"----"); }while(node.getNextNodeF()!=null); } //Loop through public static void out(Node startNode,Node endNode) { outLinked(startNode); System.out.println(); outLinkedF(endNode); } //Insert node public static void addNode(Node preNode,Node nextNode) { Node node_add = new Node("name2.5"); node_add.setNextNode(preNode.getNextNode()); preNode.setNextNode(node_add); node_add.setNextNodeF(nextNode.getNextNodeF()); nextNode.setNextNodeF(node_add); } }class Node { private String name; private Node nextNode; private Node nextNodeF; public void setName(String name) { this.name=name; } public void setNextNode(Node nextNode) { this.nextNode=nextNode; } public void setNextNodeF(Node nextNodeF) { this.nextNodeF=nextNodeF; } public String getName() { return this.name; } public Node getNextNode() { return this.nextNode; } public Node getNextNodeF() { return this.nextNodeF; } public Node(String name) { this.name=name; this.nextNode=null; } public Node( ) { } }1. Constructing a node node requires two pointers, one for forward storing the position of the next element and the other for reverse storing the position of the next element.
Parameter description:
name: used to store the node's own information
nextNode: used to store forward pointers
nextNodeF: used to store reverse pointers
class Node { private String name; private Node nextNode; private Node nextNodeF; public void setName(String name) { this.name=name; } public void setNextNode(Node nextNode) { this.nextNode=nextNode; } public void setNextNodeF(Node nextNodeF) { this.nextNodeF=nextNodeF; } public String getName() { return this.name; } public Node getNextNode() { return this.nextNode; } public Node getNextNodeF() { return this.nextNodeF; } public Node(String name) { this.name=name; this.nextNode=null; } public Node( ) { } }2. Create a node and set a pointer to connect to the node
Forward pointer: Point to the next node
Reverse node: Point to the previous node
//Construct a single linked list static Node node1 = new Node("name1"); static Node node2 = new Node("name2"); static Node node3 = new Node("name3"); static Node node4 = new Node("name4"); static Node node5 = new Node("name5"); public static void setPoint() { //Set the forward pointer node1.setNextNode(node2); node2.setNextNode(node3); node3.setNextNode(node4); node4.setNextNode(node5); //Set the reverse pointer node5.setNextNodeF(node4); node4.setNextNodeF(node3); node3.setNextNodeF(node2); node2.setNextNodeF(node1); }3. Loop the linked list through the output
public static void outLinked(Node startNode){ Node node= new Node(); node.setNextNode(startNode); do { node=node.getNextNode(); System.out.print(node.getName()+"----"); }while(node.getNextNode()!=null); } public static void outLinkedF(Node endNode){ Node node= new Node(); node.setNextNodeF(endNode); do { node=node.getNextNodeF(); System.out.print(node.getName()+"----"); }while(node.getNextNodeF()!=null); }4. Add nodes
public static void addNode(Node preNode,Node nextNode) { Node node_add = new Node("name2.5"); node_add.setNextNode(preNode.getNextNode()); preNode.setNextNode(node_add); node_add.setNextNodeF(nextNode.getNextNodeF()); nextNode.setNextNodeF(node_add); }5. Delete nodes
node2.setNextNode(node3);node3.setNextNodeF(node2);
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.