In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Java how to achieve the lead node single linked list, the article is very detailed, has a certain reference value, interested friends must read it!
Characteristics of linked lists
1, stored in the form of nodes, is a chain structure.
2, each node contains the data field, and the next field: points to the next node.
3, each node of the linked list is not necessarily continuous storage.
4. The linked list is divided into two types: leading node and non-leading node.
Realization principle
Add nodes: first traverse the original linked list, find the last node, and add the additional nodes to the end of the node. Here's how to find the last node.
The idea is that we first traverse the entire linked list and define an auxiliary variable temp, which is used to temporarily store the traversed nodes. First, assign the head header node to the temp (traverse from the header node), and traverse the node's next continuously through an endless loop until temp.next==null, the node temp is the last node in the linked list. You only need to point the next of this node to the new node.
Modify the node: first traverse the entire linked list, through the incoming number to match the number of the original linked list, find the corresponding number to replace the data in the node.
Delete a node: as shown in the figure, to delete a node, you need to traverse the entire linked list, find the corresponding number of the node, and point the next of the previous node to the node after the node to be deleted, that is, (temp.next = temp.next.next). Because the deleted node is not referenced, it will be reclaimed by the garbage collection mechanism.
Main code
Implementation of package cn.mrlij.linkedlist;/*** * single linked list * @ author dreamer * * / public class SingleLinkedList {public static void main (String [] args) {SingleLinkedListDemo s = new SingleLinkedListDemo (); HeroNode H2 = new HeroNode (1, "Song Jiang", "timely rain"); HeroNode h3 = new HeroNode (3, "Lu Junyi", "Yuqilin"); HeroNode h4 = new HeroNode (4, "Wu Yong", "Zhi Duoxing") HeroNode h5 = new HeroNode (2, "Lin Chong", "Leopard head"); s.addByOrder (H2); s.addByOrder (h3); s.addByOrder (h4); s.addByOrder (h5); System.out.println ("before modification"); s.list (); / / HeroNode H6 = new HeroNode (4, "useful", "superstar"); / / s.update (h6); s.del (1); s.del (4); s.del (2) S.del (3); System.out.println ("after deletion -"); s.list ();} class SingleLinkedListDemo {/ / create a header node, initialize the data, do not move the header node, and do not put the specific data private HeroNode head = new HeroNode (0, ",") / / add hero public void add (HeroNode node) {/ / find the last node first, and put the newly added node behind the last node HeroNode temp = head; while (true) {if (temp.next = = null) {break;} temp = temp.next;} temp.next = node;} public void addByOrder (HeroNode node) {HeroNode temp = head; boolean flag = false; while (true) {if (temp.next = = null) {break } if (temp.next.no > node.no) {break;} else if (temp.next.no = = node.no) {flag = true; break;} temp = temp.next;} if (flag) {System.out.println ("number" + node.no+ "already exists!") ;} else {node.next = temp.next; temp.next = node;}} public void update (HeroNode node) {if (head.next = = null) {System.out.println ("linked list is empty!") ; return;} HeroNode temp = head.next; boolean flag = false; while (true) {if (temp = = null) {break;} if (temp.no = = node.no) {flag = true; break;} temp = temp.next;} if (flag) {temp.name = node.name; temp.nickname = node.name;} else {System.out.println ("this node does not exist!") ;}} / / Delete node public void del (int no) {if (head.next = = null) {System.out.println ("linked list is empty!") ; return;} HeroNode temp = head; boolean flag = false; while (true) {if (temp.next = = null) {break;} if (temp.next.no = = no) {flag = true; break;} temp = temp.next;} if (flag) {temp.next = temp.next.next;} else {System.out.println ("this node does not exist!") ;} public void list () {HeroNode temp = head; if (temp.next = = null) {System.out.println ("linked list is empty!") ; return;} while (true) {if (temp.next = = null) {break;} System.out.println (temp.next); temp = temp.next;}} class HeroNode {public int no;// hero number public String name;// nickname public HeroNode next;// next node public HeroNode (int no, String name, String nickname) {this.no = no; this.name = name; this.nickname = nickname } @ Override public String toString () {return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";}} these are all the contents of the article "how to implement a single linked list of leading nodes in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.