数据结构中如何对列表进行排序

发布于 2025-01-14 10:47:10 字数 767 浏览 3 评论 0原文

我需要在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文