Java 使用冒泡排序的链表排序

Java 使用冒泡排序的链表排序,java,algorithm,linked-list,Java,Algorithm,Linked List,我正在尝试使用冒泡排序对链接列表进行排序。但是我编写的算法不起作用。有人能帮我吗 还有一个链接类 链接类 public class Link { public int iData; public String sData; public Link next; public Link(int id,String sd) { iData =id; sData =sd; next = null; } public voi

我正在尝试使用冒泡排序对链接列表进行排序。但是我编写的算法不起作用。有人能帮我吗

还有一个链接类

链接类

public class Link {
    public int iData;
    public String sData;
    public Link next;

    public Link(int id,String sd)
    {
    iData =id;
    sData =sd;
    next = null;
    }
    public void displayLink()
    {
    System.out.println(iData+""+sData); 
    }
    }
LinkKist类包括排序算法

public class LinkedList {

    private Link first;

    public void LinkList() {

        first = null;

    }

  public void insertFirst(int idata, String sdata) {
        Link nl1 = new Link(idata, sdata);

        nl1.next = first;
        first = nl1;
    }

public void displayList() {
    System.out.println("List : ");
    Link current = first;
    while (current != null) {
        current.displayLink();
        current = current.next;
    }
    System.out.println("");
}

    public void sortll(){


        Link current = first;
        Link nextLink = first.next;

        while(current.next != null){

            while(nextLink.next != null)

            if(nextLink.iData < current.iData){

                Link temp = nextLink;
                nextLink = current;
                current = temp;

                nextLink = nextLink.next;
                current = current.next;

            }

        current = current.next;
        }


    }
}

有人能帮我吗????

你的排序算法有问题,我觉得你没有进行冒泡排序。我添加了sortl()方法,它交换链表数据,但不交换链接。我认为你正在尝试交换链接,如果你这样做,那么你需要在交换链接时更加小心,因为你需要仔细考虑结束情况。替换sortll()方法并检查它

public void sortll(){


    Link current = first;
     System.out.println(first.iData);
    Link nextLink = first.next;
    /*while(current.next != null){

        while(nextLink.next != null)

        if(nextLink.iData < current.iData){

            Link temp = nextLink;
            nextLink = current;
            current = temp;

            nextLink = nextLink.next;
            current = current.next;

        }

    current = current.next;
    }*/
    int length=0;
    while(current!=null)
    {
        length++;
        current=current.next;
    }
    System.out.println(length);

    for(int i=0;i<length;i++)
    {
         Link temp=first;
        for(int j=0;j<length-i-1;j++)
        {
            if(temp.iData>temp.next.iData)
            {
                int tempiData = temp.iData;
                String tempsData =temp.sData;
                temp.iData =temp.next.iData;
                temp.sData =temp.next.sData;
                temp.next.iData=tempiData;
                temp.next.sData=tempsData;
            }
            temp=temp.next;
        }
    }


}
}
public void sortll(){
链路电流=第一;
System.out.println(first.iData);
Link nextLink=first.next;
/*while(current.next!=null){
while(nextLink.next!=null)
if(nextLink.iData对于(inti=0;i这适用于上述问题

public class LinkedList {

        private Link first;

        public void LinkList() {

            first = null;

        }

    public void sortingLinkList(){   //working 

        boolean flag = true;
        while (flag) {
            flag = false;

            Link position = first;
            Link positionNext = position.next;
            Link positionPrev = null;

            while (positionNext != null) {
                if(position.iData > positionNext.iData) {

                    Link temp = position;
                    Link tempNextNext = positionNext.next;
                    position = positionNext;
                    position.next = temp;
                    positionNext = temp;
                    positionNext.next = tempNextNext;

                    if (positionPrev == null) { // position is head
                        first = position;
                    } else {
                        positionPrev.next = position;
                    }

                    flag = true;
                }
                positionPrev = position;
                position = position.next;
                positionNext = position.next;
            }
        }
        }
    }

这里有一个关于如何调试小程序的有用链接:你能帮我解决这个问题吗???除了别人为你调试代码之外,你还期待什么样的帮助?
public class LinkedList {

        private Link first;

        public void LinkList() {

            first = null;

        }

    public void sortingLinkList(){   //working 

        boolean flag = true;
        while (flag) {
            flag = false;

            Link position = first;
            Link positionNext = position.next;
            Link positionPrev = null;

            while (positionNext != null) {
                if(position.iData > positionNext.iData) {

                    Link temp = position;
                    Link tempNextNext = positionNext.next;
                    position = positionNext;
                    position.next = temp;
                    positionNext = temp;
                    positionNext.next = tempNextNext;

                    if (positionPrev == null) { // position is head
                        first = position;
                    } else {
                        positionPrev.next = position;
                    }

                    flag = true;
                }
                positionPrev = position;
                position = position.next;
                positionNext = position.next;
            }
        }
        }
    }