Java 如何对链表排序

Java 如何对链表排序,java,sorting,linked-list,nodes,Java,Sorting,Linked List,Nodes,我使用节点创建了一个链表,该节点打印出一个名称列表(字符串)。我试图写一个排序方法,按字母顺序打印出名字。如果这是整数,这将很容易,但我不知道如何去做这件事。任何帮助或提示都将不胜感激。 -我无法使用内置集合。sort()。我需要制定自己的方法 SortMethod:(这是我在打印数字列表时使用的方法,但现在它是字符串,所以我知道我不能使用>操作符。我只是不知道如何替换它 public void Sort( BigNode front) { BigNod

我使用节点创建了一个链表,该节点打印出一个名称列表(字符串)。我试图写一个排序方法,按字母顺序打印出名字。如果这是整数,这将很容易,但我不知道如何去做这件事。任何帮助或提示都将不胜感激。 -我无法使用内置集合。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;
    }
公共类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);

    } 

}
公共字符串数据项;
公共大节点下一步;
大节点锋;
大节点电流;
字符串a=“1”、b=“2”、c=“3”、d=“4”;
字符串[]列表长度;
public void initList(){
front=null;
}
公共BigNode makeNode(字符串编号){
大节点新节点;
newNode=newbignode();
newNode.dataitems=编号;
newNode.next=null;
返回newNode;
}
公共布尔值isListEmpty(BigNode前端){
布尔平衡;
if(front==null){
平衡=正确;
}
否则{
平衡=假;
}
收益余额;
}
公共BigNode findTail(BigNode前端){
大节点电流;
电流=前沿;
while(current.next!=null){
//系统输出打印(当前数据项);
当前=当前。下一步;
}//System.out.println(current.dataitems);
回流;
}
public void addNode(BigNode前面,字符串名称){
大节尾;
如果(isListEmpty(前)){
this.front=makeNode(名称);
} 
否则{
尾部=尾翼(前部);
tail.next=makeNode(名称);
}
}
public void addAfter(BigNode n,String dataItem2){//n可能需要在前面
bignodetemp,newNode;
温度=n.next;
newNode=makeNode(dataItem2);
n、 next=newNode;
newNode.next=temp;
}
公共void findNode(BigNode前端,字符串值){
布尔查找、搜索;
大节点电流;
curr=正面;
发现=错误;
搜索=真;
while((!found)&(search)){
if(curr==null){
搜索=假;
}
else if(curr.dataitems==value){//不确定dataitems是否应该存在(.notes中的数据)
发现=真;
}
否则{
curr=curr.next;
}
}   
系统输出打印项次(当前数据项);
}
公共void deleteNode(BigNode前端,字符串值){
BigNode curr,previous=null;找到布尔值;
如果(!isListEmpty(前)){
curr=正面;
发现=错误;
while((curr.next!=null)&&(!found)){
if(当前数据项等于(值)){
发现=真;
} 
否则{
先前=当前;
curr=curr.next;
}
}
如果(!找到){
if(当前数据项等于(值)){
发现=真;
}
}
如果(找到){
如果(curr.dataitems.equals(front.dataitems)){//front.dataitems可能错误。dataitems
前=当前下一个;
}否则{
previous.next=curr.next;
}
}否则{
System.out.println(“未找到节点!”);
//curr.next=null;//不确定是否需要
}
} 
展示名单(前);
}
公共void打印节点(字符串[]len){
listlength=len;
int j;
对于(j=0;j
first.dataitems.compareTo(second.dataitems)
String.compareTo
允许您以有意义的方式比较字符串。(我假设这是家庭作业,应该这样标记。)此外,String.compareTo()的结果将允许您决定使用哪个数字排序逻辑分支-您只需使用string.compareTo()将整数比较更改为字符串比较对linkedList排序的最佳方法是首先创建一个数组并对其进行排序。您显然可以对此进行一点优化,但基本上需要数组来获得O(n log n)算法,否则你会得到O(n^2)@Voo不确定你是如何得出这个结论的。我实际上听到了O
   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);

    } 

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