Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Java single linked list

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report