This article shares the relevant code for Java to implement single-linked lists and two-way linked lists for your reference. The specific content is as follows
Java implements single linked list:
package code;class Node{ Node next; int data; public Node(int data) { this.data=data; } }class LinkList{ Node first; //head public LinkList() { this.first=null; } public void addNode(Node no) { no.next=first; first=no;//add in the head} public void delectNode() { Node n=first.next; first=null; first=n;//Delete in the head} //Delete the specified position public int Number() { int count=1; //See how many elements there are Node nd=first; while(nd.next!=null) { nd=nd.next; count++; } return count; } public void delectExact(int n) { //Delete the specified position if(n>1) { int count=1; Node de=first; while(count<n-1) { de=de.next; count++; } de.next=de.next.next; } else first=first.next; } public void addExact(int n,Node nd) { if(n>1)//Add the specified position { int count=1; Node de=first; while(count<n-1) { de=de.next; count++; } nd.next=de.next; de.next=nd; } else first=first.next; } public int findNode(int n) { int count=1;//Find the position corresponding to a number Node de=first; while(de.data!=n) { de=de.next; count++; if(de==null) { return -1; } } return count; } public void print() { Node no=first;//Print all while(no!=null) { System.out.println(no.data); no=no.next; } }}public class TextNode{ public static void main(String[] args) { LinkList ll=new LinkList(); ll.addNode(new Node(12)); ll.addNode(new Node(15)); ll.addNode(new Node(18)); ll.addNode(new Node(19)); ll.addNode(new Node(20)); /*System.out.println(ll.first.data); ll.delectNode(); System.out.println(ll.first.data);*/ System.out.println(ll.Number()); ll.delectExact(3); ll.addExact(3, new Node(100)); System.out.println(ll.Number());// ll.print(); System.out.println(ll.findNode(112)); }}Java implements two-way linked list:
public class DoubleLink{ public static void main(String[]args) { Node2 no=new Node2(5); no.addLeft(new Node2(6)); no.addRight(new Node2(7)); /*no.print(); no.print2();*/ no.addExact2(1, new Node2(8)); no.print(); System.out.println("-------------------"); no.print2(); }}class Node2{ public Node2 first; public Node2 end; public Node2 left; public Node2 right; int data=0; public Node2(int n) { first=this; end=this; first.data=n; } //Add public void from the head addLeft(Node2 before) { first.left=before; before.right=first; first=before; } //Add public void from the tail addRight(Node2 after) { end.right=after; after.left=end; end=after; } //Insert the number of public void addExact(int n,Node2 no) { int count=0; if(n==0) { addLeft(no); } else { Node2 f=first; while(true) { f=f.right; count++; if(count==n) { //This is the change in the pointing of the four pointers no.left=f.left; f.left.right=no; // first.left=no; no.right=f; f.left=no; break; } } } } } // Insert the number of public void addExact2(int n,Node2 no) { int count=0; if(n==0) { addRight(no); } else { Node2 f=end; while(true) { f=f.left; count++; if(count==n) { no.left=f; no.right=f.right; f.right.left=no; f.right=no; break; } } } } } //Positive traversal public void print() { System.out.println(first.data); while(first.right!=null) { System.out.println(first.right.data); first=first.right; }// System.out.println(end.data); } //Reverse order traversal public void print2() { System.out.println(end.data); while(end.left!=null) { System.out.println(end.left.data); end=end.left; } } }/*It is worth noting that every time a new object is inserted, you need to pay attention to the change of pointer pointing. First, the pointing of both sides of this new object (left and right), followed by the pointing of the left object to the right and the pointing of the right object to the left. The pointing of these four pointers must be correct, otherwise it may lead to the inability to implement positive or reverse order traversal. *//*Compare single linked lists, single linked lists can only be traversed from one direction because there is only one head, while a bidirectional linked list has a head and a tail. It can be traversed from the * head or from the tail. Moreover, because one of the objects has pointers in two directions, it can obtain the * object on the left and the object on the right. * But for single-linked lists, because there is only one direction, they can only go left or right. When adding objects, you can also add them from the beginning or from the end. * If a single linked list needs to add two directions, it is rare, or not, because it only has pointers to one direction to the left or right*, while each object of a bidirectional linked list has pointers to two directions, which is not more flexible, but this also has disadvantages, because in this way, each object* will contain two pointers, which will also consume more memory. * * */The above is all about this article, I hope it will be helpful for everyone to learn Java programming.