如何对链接列表进行排序

发布于 2024-12-09 05:25:09 字数 5612 浏览 0 评论 0原文

我使用打印出名称列表(字符串)的节点创建了一个链接列表。我正在尝试编写一种按字母顺序打印名称的排序方法。如果这是整数,那就很容易了,但我不知道如何去做。任何帮助或提示表示赞赏。 -我无法使用内置的collections.sort()。我需要制定自己的方法。

SortMethod:(这是我在打印数字列表时使用的,但现在是字符串,所以我知道我不能使用 > 运算符。我只是不知道如何替换它。

     public void Sort( BigNode front)
    {
            BigNode first,second,temp;
            first= front;
               while(first!=null) {

                    second = first.next;

          while(second!=null) {

    if(first.dataitems < second.dataitems)
    {
    temp = new BigNode();
    temp.dataitems =first.dataitems;
    first.dataitems = second.dataitems;
    second.dataitems = temp.dataitems;

    }
    second=second.next;
    }

这是程序的其余部分(我认为不需要,但以防万一)

          import java.util.*;

public class BigNode {

   public String dataitems; 
    public BigNode next; 
    BigNode front ;
    BigNode current;

    String a = "1", b = "2", c = "3", d = "4";
    String[] listlength;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String name){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(name);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(name);
        }
    }

    public void addAfter(BigNode n, String dataItem2) { // n might need to be front
        BigNode temp, newNode;
        temp = n.next;
        newNode = makeNode(dataItem2);
        n.next = newNode;
        newNode.next = temp;


    }

    public void findNode(BigNode front, String value) {

        boolean found , searching;
        BigNode curr;
        curr = front;
        found = false;
        searching = true;
            while ((!found) && (searching)) {
                if (curr == null) {
                    searching = false;
                }
                else if (curr.dataitems == value) { // Not sure if dataitems should be there (.data in notes)
                    found = true;

                }
                else {
                    curr = curr.next;

                }
            }   
            System.out.println(curr.dataitems);

    }


    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.next = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){
        listlength = len;

        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

          public void showList(BigNode front){

        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
        MenuOptions();
    }

    public void MenuOptions() {
        Scanner in = new Scanner(System.in);
        System.out.println("Choose an Option Below:");
        System.out.println(a + "(Display Length of List)" + ", " + b + "(Delete a person)" + ", " + c + ("(Exit)"));
        String pick = in.nextLine();

        if (pick.equals(b)) {
            System.out.println("Who would you like to delete?");

             String delete = in.nextLine();
             deleteNode(front, delete);

        }

         if (pick.equals(a)) {
            System.out.println("Length of List = " + listlength.length);
            MenuOptions();
        } 

         if (pick.equals(c)) {

         }

    }



    public static void main(String[] args) {


        String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue", "Malcom Floyd"}; // Trying to print theses names..Possibly in alphabetical order

        BigNode x = new BigNode(); 

        x.printNodes(names);

    } 

}

I created a linked list using nodes that prints out a list(string) of names. I'm trying to write a sort method that prints out the names in alphabetical order. If this was integers it would be easy but I have no idea how to go about doing this. Any help or tips are appreciated.
-I cannot use the builtin collections.sort(). I need to make my own method.

SortMethod: (this is what I used when I was printing a list of numbers out but now its strings so I know I can't use the > operator. I just don't know how to replace it.

     public void Sort( BigNode front)
    {
            BigNode first,second,temp;
            first= front;
               while(first!=null) {

                    second = first.next;

          while(second!=null) {

    if(first.dataitems < second.dataitems)
    {
    temp = new BigNode();
    temp.dataitems =first.dataitems;
    first.dataitems = second.dataitems;
    second.dataitems = temp.dataitems;

    }
    second=second.next;
    }

Heres the rest of program(I dont think its needed but just in case)

          import java.util.*;

public class BigNode {

   public String dataitems; 
    public BigNode next; 
    BigNode front ;
    BigNode current;

    String a = "1", b = "2", c = "3", d = "4";
    String[] listlength;

    public void initList(){
        front = null;
    }

    public BigNode makeNode(String number){
        BigNode newNode;
        newNode = new BigNode();
        newNode.dataitems = number;
        newNode.next = null;
        return newNode;
    }

    public boolean isListEmpty(BigNode front){
        boolean balance;
        if (front == null){
            balance = true;
        }
        else {
            balance = false;
        }
        return balance;

    }

    public BigNode findTail(BigNode front) {
        BigNode current;
        current = front;
        while(current.next != null){
            //System.out.print(current.dataitems);
            current = current.next;

        } //System.out.println(current.dataitems);
        return current;
    }

    public void addNode(BigNode front ,String name){
        BigNode tail;
        if(isListEmpty(front)){
            this.front = makeNode(name);
        } 
        else {
            tail = findTail(front);
            tail.next = makeNode(name);
        }
    }

    public void addAfter(BigNode n, String dataItem2) { // n might need to be front
        BigNode temp, newNode;
        temp = n.next;
        newNode = makeNode(dataItem2);
        n.next = newNode;
        newNode.next = temp;


    }

    public void findNode(BigNode front, String value) {

        boolean found , searching;
        BigNode curr;
        curr = front;
        found = false;
        searching = true;
            while ((!found) && (searching)) {
                if (curr == null) {
                    searching = false;
                }
                else if (curr.dataitems == value) { // Not sure if dataitems should be there (.data in notes)
                    found = true;

                }
                else {
                    curr = curr.next;

                }
            }   
            System.out.println(curr.dataitems);

    }


    public void deleteNode(BigNode front, String value) {
        BigNode curr, previous = null; boolean found; 

            if (!isListEmpty(front)){
                curr = front;
                found = false;

                while ((curr.next != null) && (!found)) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    } 
                    else {
                        previous = curr;
                        curr = curr.next;
                    }
                }
                if (!found) {
                    if(curr.dataitems.equals(value)) {
                        found = true;
                    }
                }
                if (found) {
                    if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
                        front = curr.next;
                    } else { 

                        previous.next = curr.next;
                    }
                } else {
                    System.out.println("Node not found!");
                    //curr.next = null; // Not sure If this is needed
                }
        } 
            showList(front);
    }




    public void printNodes(String[] len){
        listlength = len;

        int j;
        for (j = 0; j < len.length; j++){

            addNode(front, len[j]);
        }  showList(front);
    }

          public void showList(BigNode front){

        current = front;
        while ( current.next != null){
            System.out.print(current.dataitems + ", ");
            current = current.next;
        }
        System.out.println(current.dataitems);
        MenuOptions();
    }

    public void MenuOptions() {
        Scanner in = new Scanner(System.in);
        System.out.println("Choose an Option Below:");
        System.out.println(a + "(Display Length of List)" + ", " + b + "(Delete a person)" + ", " + c + ("(Exit)"));
        String pick = in.nextLine();

        if (pick.equals(b)) {
            System.out.println("Who would you like to delete?");

             String delete = in.nextLine();
             deleteNode(front, delete);

        }

         if (pick.equals(a)) {
            System.out.println("Length of List = " + listlength.length);
            MenuOptions();
        } 

         if (pick.equals(c)) {

         }

    }



    public static void main(String[] args) {


        String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue", "Malcom Floyd"}; // Trying to print theses names..Possibly in alphabetical order

        BigNode x = new BigNode(); 

        x.printNodes(names);

    } 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2024-12-16 05:25:09
first.dataitems.compareTo(second.dataitems)<0

或者如果你有一个比较器

comp.compare(first.dataitems, second.dataitems)<0
first.dataitems.compareTo(second.dataitems)<0

or if you have a comparator

comp.compare(first.dataitems, second.dataitems)<0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文