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/01 Report--
This article "how to use Java to achieve the addition, deletion, modification and search of a single linked list" is not understood by most people, so the editor summarizes the following, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to use Java to achieve the addition, deletion, modification and query of a single linked list" article.
I. create a new student node class
The Stu_Node node contains:
Student number: int num
Name: String name
Gender: String gender
Next node: Stu_Node next
The toString method needs to be overridden to make it easier to print node content.
Class Stu_Node {int num; String name; String gender; Stu_Node next; @ Override public String toString () {return "Stu_Node {" + "num=" + num + ", name='" + name +''+ ", gender='" + gender +''+'}' } public Stu_Node (int num, String name, String gender) {this.num=num; this.name=name; this.gender=gender;}} 2. Create a new class to operate the linked list and implement the method of adding, deleting, modifying and searching.
1 > create the implementation class object of the linked list
Class SingleLinkedList {}
2 > implement the method of adding linked list nodes to the tail node in the class
1. Define an empty header node head.
2. Define a node variable temp equal to the header node head.
3. Traverse the linked list to find the tail node.
While loop, the end sign is that the next node of the temp is empty, and the while is terminated (otherwise the program will fall into a dead loop). Each time the loop is performed, the next node of the temp node will be assigned to the temp variable (otherwise the program will fall into a dead loop).
4. Point the next node of the temp node to the new node.
Class SingleLinkedList {Stu_Node head = new Stu_Node (0, "null", "null"); / / the method of adding the tail node of the linked list public void add (Stu_Node node) {Stu_Node temp = head; while (true) {if (temp.next==null) {break;} temp = temp.next;} temp.next=node }}
Insert nodes in the order of student numbers
1. Create a new temporary node temp
2. Create a new boolean variable flag to find the current location with the inserted node.
3. While cycle through the linked list to find the location of the insertion node. If the next node of temp is empty, the loop is terminated; if the student number of the next node of temp is equal to the student number of the node to be inserted, the flag variable is assigned to true to end the cycle (the node with insertion has already failed to insert) If the student number of the next node of the temp node is greater than the student number with the inserted node, the location of the next node of the temp is the location to be inserted. Each time the linked list is traversed, the node assigns the next node of the temp to the temp node to avoid an endless loop.
4. If flag is true, the node to be inserted already exists and the output insertion fails; if flag is false, the next node of the inserted node points to the next node of the temp, and the next node of the temp node points to the inserted node. (drawing is easy to understand, and the picture is self-filling.)
Public void addOrder (Stu_Node node) {Stu_Node temp = head; boolean flag = false; while (true) {if (temp.next==null) break; if (temp.next.num > node.num) {break;} else if (temp.next.num==node.num) {flag = true Break;} temp=temp.next;} if (flag) {System.out.println ("insert failed");} else {node.next=temp.next; temp.next=node;}
3 > print linked list
1. Determine whether the next node of the header node is empty, and if so, the output linked list is empty.
2. Define a temp node variable and assign the node pointed to by the header node to temp.
3. Traverse the linked list to print node data.
While loop, print the linked list node, print each temp node, so that the next node of the temp node is assigned to the temp to prevent the program from falling into a dead loop, and the end of the loop is marked that the node of temp is empty (because the temp node is the next node of the header node head).
Class SingleLinkedList {Stu_Node head = new Stu_Node (0, "null", "null"); / / print single linked list public void print () {if (head.next==null) {System.out.println ("linked list is empty");} Stu_Node temp= head.next; while (true) {if (temp==null) {break } System.out.println (temp); temp=temp.next;}
4 > modify the data in the linked list (assuming the student number is one-dimensional)
1. First determine whether the linked list is empty
2. Define a temporary variable temp to assign the header node to temp
3. Define a Boolean variable flag to determine whether there is a node to be modified in the linked list.
4. Traversing the linked list, the while loop queries the node to be modified. Each query assigns the temp variable to the next node of temp. If the node to be modified assigns the flag variable to true, the while loop is terminated. If the next node of temp is empty, the loop is also terminated.
5. Determining the value of flag as true means finding the current node to be modified, and assigning the value of the new node to the value of the node to be modified; if the value is false, the current node is not found.
Class SingleLinkedList {Stu_Node head = new Stu_Node (0, "null", "null"); / / the modification method of the linked list public void update (Stu_Node newnode) {if (head.next==null) {System.out.println ("the linked list is empty");} Stu_Node temp = head; boolean flag = false While (true) {if (temp.next==null) {break;} if (temp.num== newnode.num) {flag = true; break;} temp=temp.next;} if (flag) {temp.name=newnode.name Temp.gender= newnode.gender;} else System.out.println ("current node does not exist and cannot be modified!") ;}}
5 > Delete nodes according to the student number
1. Define a temporary node temp and assign the header node to temp.
2. Define a Boolean variable flag to determine whether to find the node to be deleted in the current linked list.
3. The while loop traverses the current linked list, traversing the node once, assigning the next node of the temp node to temp to avoid the loop. If the student number of the node to be deleted is the same as the student number of the node to be deleted, assign flag to true to terminate the while loop, otherwise continue to traverse the linked list until the next node of the temp node is empty.
4. If flag is true, it means to find the current node to be deleted, and point the next node of temp to the next node of temp. If the flag is false, the node to be deleted cannot be found.
Class SingleLinkedList {Stu_Node head = new Stu_Node (0, "null", "null"); / / delete the node of the linked list public void delete (int no) {Stu_Node temp = head; boolean flag= false; while (true) {if (temp.next==null) break; if (temp.next.num==no) {flag=true Break;} temp = temp.next;} if (flag) {temp.next=temp.next.next;} else {System.out.println ("no linked list found to be deleted"); 3. Create an implementation class of the operation chain list class to implement its function of adding, deleting, modifying and querying.
1. Create the implementation class object of the operation chain list class.
2. Create multiple nodes to be operated
3. Use the newly created linked list operation class object to add, delete, modify and query.
The complete code is as follows:
Public class linkedlist {public static void main (String [] args) {SingleLinkedList list = new SingleLinkedList (); Stu_Node node1 = new Stu_Node (1903210086, "Xiaoming", "male"); Stu_Node node2 = new Stu_Node (1903210087, "floret", "female"); Stu_Node node3 = new Stu_Node (1903210088, "Xiao Huang", "male") Stu_Node node4 = new Stu_Node (1903210089, "Xiao Cui", "female"); list.add (node1); list.add (node4); list.add (node3); list.add (node2); list.print (); System.out.println ("-") System.out.println ("insert nodes in order of student number"); SingleLinkedList list_1= new SingleLinkedList (); list_1.addOrder (node1); list_1.addOrder (node4); list_1.addOrder (node3); list_1.addOrder (node2); list_1.print () System.out.println ("- -"); Stu_Node node5 = new Stu_Node (1903210089, "Jia Ming", "male"); list.update (node5); list.print (); System.out.println ("# #") System.out.println ("deleted linked list"); list.delete (1903210088); list.print ();} class Stu_Node {int num; String name; String gender; Stu_Node next @ Override public String toString () {return "Stu_Node {" + "num=" + num + ", name='" + name +''+ ", gender='" + gender +''+'}';} public Stu_Node (int num, String name, String gender) {this.num=num; this.name=name This.gender=gender;}} class SingleLinkedList {Stu_Node head = new Stu_Node (0, "null", "null"); / / the method of adding the tail node of the linked list public void add (Stu_Node node) {Stu_Node temp = head; while (true) {if (temp.next==null) {break;} temp = temp.next } temp.next=node;} public void addOrder (Stu_Node node) {Stu_Node temp = head; boolean flag = false; while (true) {if (temp.next==null) break; if (temp.next.num > node.num) {break } else if (temp.next.num==node.num) {flag = true; break;} temp=temp.next;} if (flag) {System.out.println ("insert failure");} else {node.next=temp.next; temp.next=node } public void print () {if (head.next==null) {System.out.println ("linked list is empty");} Stu_Node temp= head.next; while (true) {if (temp==null) {break;} System.out.println (temp); temp=temp.next } public void update (Stu_Node newnode) {if (head.next==null) {System.out.println ("linked list is empty");} Stu_Node temp = head; boolean flag = false; while (true) {if (temp.next==null) {break } if (temp.num== newnode.num) {flag = true; break;} temp=temp.next;} if (flag) {temp.name=newnode.name; temp.gender= newnode.gender } else System.out.println ("current node does not exist and cannot be modified!") ;} public void delete (int no) {Stu_Node temp = head; boolean flag= false; while (true) {if (temp.next==null) break; if (temp.next.num==no) {flag=true; break;} temp = temp.next } if (flag) {temp.next=temp.next.next;} else {System.out.println ("linked list not found to delete") } the above is about "how to use Java to add, delete, modify and query single linked list". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to 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.