数据结构中如何对列表进行排序
我需要在java中创建一个对列表进行排序的算法,我已经创建了创建列表的部分,但我无法对其进行排序,我尝试添加冒泡排序和合并排序,但它们都不起作用,在这里是我的代码,我非常感谢您的帮助。
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class No{
int number;
No next;
public No(int number){
this.number = number;
}
}
class Lista{
No head;
public void add(int n){
if(head == null){
head = new No(n);
}else{
No temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = new No(n);
}
}
public String toString(){
No temp = head;
String str = "";
while(temp != null){
str += temp.number+" ";
temp = temp.next;
}
return str;
}
}```
I need to create an algorithm in java that sorts a list, I've created the part where the list is made, but I can't sort it, I have tried to add bubble sort and merge sort but none of them worked, here is my code, I would really appreciate the help.
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class No{
int number;
No next;
public No(int number){
this.number = number;
}
}
class Lista{
No head;
public void add(int n){
if(head == null){
head = new No(n);
}else{
No temp = head;
while(temp.next != null){
temp = temp.next;
}
temp.next = new No(n);
}
}
public String toString(){
No temp = head;
String str = "";
while(temp != null){
str += temp.number+" ";
temp = temp.next;
}
return str;
}
}```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论