How to realize Java single linked list
This article mainly explains "Java single linked list how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java single linked list how to achieve" it!
Public class Linked {private Node head; public void addNode (Node node) {if (null = = head) {head = node; return;} Node temp = head; while (null! = temp.getNext ()) {temp = temp.getNext ();} temp.setNext (node);}
Public void delNode (Node node) {if (head.equals (node)) {head = head.getNext (); return;} Node temp = head; Node prev = temp.next; while (null! = temp.next) {if (node.equals (temp.next)) {if (null! = cur.next) {prev.next = cur.next } else {prev.next = null;} break;} temp = prev.next; prev = cur; cur = cur.next;}}
Public void updateNode (Node source, Node target) {if (source.equals (head)) {head = target;} Node temp = head; while (null! = temp.next) {Node cur = temp.next; if (cur.equals (source)) {temp.next = target;} temp = temp.next;}}
Public Node find (int index) {if (0 = = index) {return head;} Node temp = head; int curIndex = 1; while (null! = temp.next) {if (index = = curIndex) {return temp.next;} temp = temp.next;} return null;}
Public static void main (String [] args) {Linked linked = new Linked (); Node node = new Node (); node.setData ("1"); Node node2 = new Node (); node2.setData ("2"); Node node3 = new Node (); node3.setData ("3"); linked.addNode (node); linked.addNode (node2); linked.addNode (node3); / / Node updateNode = node2 / / updateNode.setData ("update"); / / linked.updateNode (node2, updateNode); Node nodeFind=linked.find (1); System.out.println (nodeFind)
}
}
Thank you for your reading, the above is the content of "how to achieve Java single linked list", after the study of this article, I believe you have a deeper understanding of how to achieve this problem of Java single linked list, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!