在Java中手动对链表进行冒泡排序

在Java中手动对链表进行冒泡排序,java,linked-list,bubble-sort,Java,Linked List,Bubble Sort,这是我的第一个问题。 我试图在java中手动排序一个整数链表,但我无法找出代码的错误。有什么建议吗?我没有得到任何错误,但我仍然有我的输出无序。我尝试了几种不同的方法,但都不管用。如果有人能帮我,我将不胜感激 public class Node { int data; Node nextNode; public Node(int data) { this.data = data; this.nextNode = null;

这是我的第一个问题。 我试图在java中手动排序一个整数链表,但我无法找出代码的错误。有什么建议吗?我没有得到任何错误,但我仍然有我的输出无序。我尝试了几种不同的方法,但都不管用。如果有人能帮我,我将不胜感激

public class Node {

    int data; 
    Node nextNode;

    public Node(int data) {
        this.data = data;
        this.nextNode = null;       
    }

    public int getData() {
        return this.data;
    }
} // Node class


public class DataLinkedList implements DataInterface {  
    private  Node head;
    private int size; 

    public DataLinkedList(){
        this.head = null;
        this.size = 0;
    }

    public void add(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
        } else {
            Node currentNode = head;
            while(currentNode.nextNode != null) {
                currentNode = currentNode.nextNode;
            }
            currentNode.nextNode = node;
        }
        size++;     
    }

    public void sort() {
        if (size > 1) {
            for (int i = 0; i < size; i++ ) {
                Node currentNode = head;
                Node next = head.nextNode;
                for (int j = 0; j < size - 1; j++) {
                    if (currentNode.data > next.data) {
                        Node temp = currentNode;
                        currentNode = next;
                        next = temp;
                    } 
                    currentNode = next;
                    next = next.nextNode;                   
                } 
            }
        }
    }

    public int listSize() {     
        return size;
    }

    public void printData() {
        Node currentNode = head;
        while(currentNode != null) {
            int data = currentNode.getData();
            System.out.println(data);
            currentNode = currentNode.nextNode;
        }
    }

    public boolean isEmpty() {
        return size == 0;
    }
} // DataInterface class

public class DataApp {

    public static void main (String[]args) {

        DataLinkedList dll = new DataLinkedList();

        dll.add(8);
        dll.add(7);
        dll.add(6);
        dll.add(4);
        dll.add(3);
        dll.add(1);

        dll.sort();
        dll.printData();
        System.out.println(dll.listSize());
    }
} // DataApp class
公共类节点{
int数据;
节点下一节点;
公共节点(int数据){
这个数据=数据;
this.nextNode=null;
}
公共int getData(){
返回此.data;
}
}//节点类
公共类DataLinkedList实现DataInterface{
专用节点头;
私有整数大小;
公共数据链接列表(){
this.head=null;
此值为0.size=0;
}
公共无效添加(整型数据){
节点=新节点(数据);
if(head==null){
头部=节点;
}否则{
节点当前节点=头;
while(currentNode.nextNode!=null){
currentNode=currentNode.nextNode;
}
currentNode.nextNode=节点;
}
大小++;
}
公共无效排序(){
如果(大小>1){
对于(int i=0;inext.data){
节点温度=当前节点;
currentNode=next;
下一个=温度;
} 
currentNode=next;
next=next.nextNode;
} 
}
}
}
public int listSize(){
返回大小;
}
public void printData(){
节点当前节点=头;
while(currentNode!=null){
int data=currentNode.getData();
系统输出打印项次(数据);
currentNode=currentNode.nextNode;
}
}
公共布尔值为空(){
返回大小==0;
}
}//数据接口类
公共类DataApp{
公共静态void main(字符串[]args){
DataLinkedList dll=新建DataLinkedList();
dll.add(8);
添加(7);
添加(6);
添加(4);
添加(3);
添加(1);
dll.sort();
dll.printData();
System.out.println(dll.listSize());
}
}//DataApp类

您需要交换数据,而不仅仅是节点

    public void sort() {
    if (size > 1) {
        for (int i = 0; i < size; i++ ) {
            Node currentNode = head;
            Node next = head.nextNode;
            for (int j = 0; j < size - 1; j++) {
                if (currentNode.data > next.data) {
                    int temp = currentNode.data;
                    currentNode.data = next.data;
                    next.data = temp;
                } 
                currentNode = next;
                next = next.nextNode;                   
            } 
        }
    }
}
if (currentNode.data > next.data) {
    int temp = currentNode.data;
    currentNode.data = next.data;
    next.data = temp;
}
public void sort(){
如果(大小>1){
对于(int i=0;inext.data){
int temp=currentNode.data;
currentNode.data=next.data;
next.data=温度;
} 
currentNode=next;
next=next.nextNode;
} 
}
}
}

正如所料,问题出现在方法
sort()

如果按
v[i]
转换currentNode,然后按
v[i-1]
转换currentNode,就像对数组排序一样,就可以了。但是,您只是在更改用于在列表上运行的指针,而不会对列表本身产生影响。最好的解决方案(如果您要使用BubbleSort,这通常是最差的解决方案)是在节点内部交换数据

    public void sort() {
    if (size > 1) {
        for (int i = 0; i < size; i++ ) {
            Node currentNode = head;
            Node next = head.nextNode;
            for (int j = 0; j < size - 1; j++) {
                if (currentNode.data > next.data) {
                    int temp = currentNode.data;
                    currentNode.data = next.data;
                    next.data = temp;
                } 
                currentNode = next;
                next = next.nextNode;                   
            } 
        }
    }
}
if (currentNode.data > next.data) {
    int temp = currentNode.data;
    currentNode.data = next.data;
    next.data = temp;
}
但是,为了说明这个概念,我将建议更改节点之间的链接。这些指针实际上是在列表中标记排序的指针。我说的是
nextNode
属性,它位于节点类中

聊够了,这里是:

public void sort() {
        if (size > 1) {
            boolean wasChanged;

            do {
                Node current = head;
                Node previous = null;
                Node next = head.nextNode;
                wasChanged = false;

                while ( next != null ) {
                    if (current.data > next.data) {
                        /*
                        // This is just a literal translation
                        // of bubble sort in an array
                        Node temp = currentNode;
                        currentNode = next;
                        next = temp;*/
                        wasChanged = true;

                        if ( previous != null ) {
                            Node sig = next.nextNode;

                            previous.nextNode = next;
                            next.nextNode = current;
                            current.nextNode = sig;
                        } else {
                            Node sig = next.nextNode;

                            head = next;
                            next.nextNode = current;
                            current.nextNode = sig;
                        }

                        previous = next;
                        next = current.nextNode;
                    } else { 
                        previous = current;
                        current = next;
                        next = next.nextNode;
                    }
                } 
            } while( wasChanged );
        }
    }
对管理节点交换的“双”代码的解释是,由于您必须更改节点之间的链接,而这只是一个单链表,因此您必须跟踪上一个节点(您也没有头节点),第一次是

if ( previous != null ) {
    Node sig = next.nextNode;

    previous.nextNode = next;
    next.nextNode = current;
    current.nextNode = sig;
} else {
    Node sig = next.nextNode;

    head = next;
    next.nextNode = current;
    current.nextNode = sig;
}
您可以在以下IdeOne中找到代码:


希望这有帮助。

你可以这样写这个方法:

class Node {
   int data = 0;
   Node next;

   public Node(int data) {
     this.data = data;
   }
}



public void sort() {

    Node current = head;
    while (current != null) {

        Node second = current.next;
        while (second != null) {

            if (current.data > second.data) {
                int tmp = current.data;
                current.data = second.data;
                second.data = tmp;
            }
            second = second.next;
        }
        current = current.next;
    }
}

实际上,您并没有更改节点内的链接;每一次。但是你不改变头。实际上,您可以将第一个元素与其他大小的元素进行比较!非常感谢您的详细解释。我真的很难把它分类。顺便说一句,我不能投票支持你的答案,因为我没有15个声誉。问题是你已经解决了。:-)如果你想的话,当你有更多的名声时,你可以随时回来。干杯。我知道,这是一个3年前的帖子,但你的回答并不重要@Baltasarq,它帮了我很大的忙。谢谢谢谢Saicharan。你们帮了大忙,谢谢。你的回答帮助我解决了双链表的bubblesort方法。