Web-wide most detailed Java implementation of single-chain list (add, delete, change, check, traverse, etc.)
Take the student management system as an example: the node is the student, and the whole chain table is the management system
Node Definition, Constructor
The student class has information such as number, name, subscript, etc. By default, all nodes are empty
For convenience. Make all information public so you don't have to write get() and set() methods
public class Student { public int number=0; public int size=0; //subscript public String name; public Student next =null; public boolean isEmpty =true; public Student(){ } public Student(int number,String name, int size){ this.name =name; this.number =number; this.size =size; this.isEmpty =false; next=null; } public Student(int number,String name){ this.name =name; this.number =number; this.isEmpty =false; next=null; }
Note: Why use student? Next for comparison? (Note that it's a little winding here)
1, if student.next ==null, then it is the last node in the chain table
2, About Delete Operation
If it's a double-linked list, use student.before =studet.next can delete the student node, but since the single-linked list does not have the before attribute, when traversing through the student node, the previous node of the student cannot be found, and this operation cannot be accomplished.
But we can do it in a new way that can be combined with picture understanding
Original: student.before =student.next
New: student...next= student.next.next
Traverse the list (print student information)
public void print(){ Student student =this; while (true){ System.out.println("Chain List Subscript"+student.size+ " School Number:"+student.number+" Name"+student.name); if (student.next ==null){ break; }//The last one student =student.next; } }
Node replication: Node calling method = new node
public void copy(Student student){ this.number = student.number; this.name =student.name; this.next =student.next; this.isEmpty =student.isEmpty; }
Add: Add student information (empty node add information, add new node)
If student. Next ==null then determine that the student is the last node in the chain table and that a new node needs to be created to store the new data
public void addStudent(Student s){ Student student =this; while (true){ if (student.isEmpty ==true){ student.name =s.name; student.number =s.number; student.isEmpty =false; break; }//There is already a new node, just add information if (student.next ==null){ student.next =s; s.size = student.size+1; s.next =null; /** * Why add this sentence? Is next not null? There are bug s and I don't know how to fix them */ break; }//The last one student =student.next; } }
Delete: Delete student information according to school number (this section is the hardest part of a list operation, if you understand, you'll have a list)
public void remove(int removeNumber){ Student student =this; while (true){ if (student.number ==removeNumber){ System.out.println("Deleted school number"+student.number+"Name"+student.name); Student newhead =student.next; if (newhead !=null){ this.copy(newhead); //Head node is not the only node }else{ System.out.println("This list has been completely deleted"); Student student1 =new Student(); this.copy(student1); }//Head node is the only node, newhead is null break; }//To delete the header node if (student.next ==null){ if (student.number!=removeNumber){ System.err.println("There is no student in the system"); } break; }//Last Node if (student.next.number == removeNumber){ Student student3 =student.next.next; System.out.println("Deleted school number"+student.next.number+"Name"+student.next.name); student.next =student3; break; }//Not deleting the header node student=student.next; } }
Delete: Delete student information by name
public void remove(String name){ Student student =this; int removeNumber =0; while (true){ if (student.name.equals(name)){ removeNumber =student.number; System.out.println("Deleted school number"+student.number+"Name"+student.name); break; } if (student.next ==null){ break; }//The last one student=student.next; } Student student2 =this; if (removeNumber ==0){ //Can't find System.err.println("There is no student in the system"); }else{ //Eureka while (true){ if (student2.number ==removeNumber){ Student head =new Student(); head.next =student2.next; // this= head; break; }//Header Node Delete if (student2.next ==null){ break; }//Last Node if (student2.next.number == removeNumber){ Student student3 =student2.next.next; student2.next =student3; break; }//Not deleting the header node student2=student2.next; } } }
Change: Change name according to school number
public void set(int newNumber, String newNme){ Student student =this; while (true){ if (student.number ==newNumber){ student.name =newNme; System.out.println("New name set successfully"); break; }//Corresponding Students if (student.next ==null){ if (student.number !=number){ System.err.println("No corresponding number"); }//Corresponding Students break; }//The last one student =student.next; } }
Check: Find a name by number
public String find(int number){ Student student =this; while (true){ if (student.number ==number){ return student.name; }//Corresponding Students if (student.next ==null){ if (student.number !=number){ System.err.println("No corresponding number"); }//Corresponding Students break; }//Last Node student =student.next; } return null; }
Modify student information based on student number (for example, change name)
public void set(int newNumber, String newNme){ Student student =this; while (true){ if (student.number ==newNumber){ student.name =newNme; System.out.println("New name set successfully"); break; }//Corresponding Students if (student.next ==null){ if (student.number !=number){ System.err.println("No corresponding number"); }//Corresponding Students break; }//The last one student =student.next; } }
Test Code Section
Test: Add, print, change name according to school number
public void test_Set(){ Student student1 = new Student(1,"zjh1"); Student student8 = new Student(8,"zjh8"); Student student15 = new Student(15,"zjh15"); student1.addStudent(student8); student1.addStudent(student15); student1.print(); String name =student1.find(8); System.out.println("Name"+name); student1.set(8,"zjhNB"); String name2 =student1.find(8); System.out.println("New name"+name2); student1.print(); }
Test: Delete students by name
public void test_Remove(){ Student student1 = new Student(1,"zjh1"); Student student2 = new Student(2,"zjh2"); Student student3 = new Student(3,"zjh3"); Student student8 = new Student(8,"zjh8"); Student student15 = new Student(15,"zjh15"); student1.addStudent(student2); student1.addStudent(student3); student1.addStudent(student8); student1.addStudent(student15); student1.print(); student1.remove("zjh8"); student1.remove("zjh15"); student1.remove(""); student1.print(); }
Summary: Chain lists themselves are not difficult, but it is important to note that linear tables and hash tables will be used to traverse the scope, especially to delete this operation.
Write for 2 days and finally finished whining. Just writing this blog for 2 hours, I feel a slow sense of accomplishment. Recently I really can't stop programming at all