In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about Java how to achieve single-linked list SingleLinkedList add, delete, change and query and reverse, reverse and other operations. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Node class
You can modify the node attributes as needed. Note that the toString () method is overridden for subsequent output operations.
/ / Node class class Node {public int id; public String name; public Node next; public Node (int id, String name) {this.id = id; this.name = name;} @ Override public String toString () {return "Node {" + "id=" + id + ", name='" + name +'\'+'}' }} linked list class (main)
The functions of adding, deleting, changing, searching, reversing, reversing and so on are basically applicable. Implementation ideas are annotated in the code.
/ / linked list class (management node) class LinkedList {/ / header node Node head = new Node; / / number of valid data in the linked list (length of linked list) (excluding header node) public int size () {Node temp = head; int size = 0; while (true) {if (temp.next = = null) {break } size++; temp = temp.next;} return size;} / / Show linked list public void list () {if (head.next = = null) {System.out.println ("linked list is empty!") ; return;} Node temp = head.next; while (true) {if (temp = = null) {break;} System.out.println (temp); temp = temp.next;}} / / increase (from small to large according to id) public void add (Node newNode) {Node temp = head While (true) {/ / is used to find the chain tail if (temp.next = = null) {break;} if (temp.id = = newNode.id) {System.out.println ("the id of the node to be added already exists, failed to add!") ; return;} if (temp.next.id > newNode.id) {break;} temp = temp.next;} Node node = newNode; newNode.next = temp.next; temp.next = node } / / delete (delete according to id match) public void remove (int id) {if (head.next = = null) {System.out.println ("linked list is empty!"); return;} Node temp = head; boolean flag = false / / used to mark whether the node corresponding to the id while (true) {if (temp.next = = null) {break;} if (temp.next.id = = id) {/ / find the previous node to delete the node flag = true; break;} temp = temp.next } if (flag) {temp.next = temp.next.next;} else {System.out.println ("Node not found for deletion, deletion failed!") }} / / change (match the node to be modified according to id) public void update (int id,String name) {if (head.next = = null) {System.out.println ("linked list is empty!") ; return;} Node temp = head; boolean flag = false; / / used to mark whether the corresponding id node while (true) {if (temp.next = = null) {break;} if (temp.id = = id) {flag = true; break } temp = temp.next;} if (flag) {temp.name = name;} else {System.out.println ("No node was found, modification failed!") ;}} / / check (match according to id) public Node show (int id) {if (head.next = = null) {System.out.println ("linked list is empty!") ; return null;} Node temp = head.next; boolean flag = false; while (true) {if (temp = = null) {break;} if (temp.id = = id) {flag = true; break;} temp = temp.next } if (flag) {return temp;} else {System.out.println ("failed to find the node you were looking for!") ; return null;}} / / find the penultimate node public Node lastShow (int n) {Node temp = head.next; int size = this.size (); if (size < n | | n 0) {System.out.println (nodes.pop ());} test class import java.util.Stack / * * @ Author: Yeman * @ Date: 2021-10-14-12:55 * @ Description: * / / Test class public class SingleLinkedListTest {public static void main (String [] args) {LinkedList linkedList = new LinkedList (); Node node1 = new Node (1, "Alan"); Node node2 = new Node (2, "Los Angeles"); Node node3 = new Node (3, "Exxon") / / you can add linkedList.add (node1), linkedList.add (node3), linkedList.add (node2), linkedList.list (), System.out.println (linkedList.size ()), / / list length / / System.out.println (linkedList.lastShow (2)) out of id order. / / countdown search / / linkedList.update (2, "Zhang Yuning"); / / change / linkedList.remove (3); / delete / System.out.println (linkedList.show (2)); / / check / / linkedList.reverse (); / / list reverse linkedList.reversePrint (); / / reverse print}} Thank you for reading! On "Java how to achieve single-linked list SingleLinkedList addition, deletion, query and reverse, reverse and other operations" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
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.