2015-04-07 126 views
0

我試圖排序使用氣泡sort.But鏈接列表,但我寫的算法不起作用。可以幫助我嗎?鏈接列表排序使用氣泡排序

還有一個鏈接類。

Link類

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; 
     } 


    } 
} 

測試應用程序。

public class LLtest { 

    public static void main(String[] args) { 

     LinkedList ll1 = new LinkedList(); 

     ll1.insertFirst(11, "UWU0011"); 
     ll1.insertFirst(3, "UWU0003"); 
     ll1.insertFirst(1, "UWU0001"); 
     ll1.insertFirst(4, "UWU0004"); 
     ll1.insertFirst(5, "UWU0005"); 
     ll1.insertFirst(6, "UWU0006"); 
     ll1.insertFirst(7, "UWU0007"); 
     ll1.insertFirst(10, "UWU0010"); 
     ll1.insertFirst(9, "UWU0009"); 
     ll1.insertFirst(2, "UWU0002"); 
     ll1.insertFirst(8, "UWU0008"); 



     ll1.sortll(); 


     ll1.displayList(); 
    } 
} 

任何人都可以請幫我?????

+3

這裏有一個關於如何有用的鏈接調試小程序:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

+0

你可以幫我解決這個問題? ??? –

+1

你期望什麼樣的幫助不僅僅是別人爲你調試你的代碼? –

回答

0

你的排序算法有問題,我覺得你沒有做冒泡排序。我添加了sortll()方法來交換鏈接列表數據,但是不交換鏈接。我認爲你正在嘗試交換鏈接,如果你這樣做,那麼你需要更加小心地交換鏈接,因爲你需要仔細考慮最終案例。替換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; 
     } 
    } 


} 
} 
0

這適用於上述問題。

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; 
      } 
     } 
     } 
    }