合并在链接列表Java上排序

时间:2017-04-19 11:12:28

标签: java

我一直在尝试将合并排序应用到链接列表中,我对我正在做的事情有一个大概的了解,但是当运行时代码失败了我的简单jUnit测试。我不确定哪个部分出了问题,似乎无法找出需要解决的问题。 任何帮助都是值得赞赏和解释,以帮助未来更多。

class musicNode {
    String track;  // The name of the track
    int played= 0; // The number of times played
    int shuffleTag= 0;
    musicNode next;

public musicNode() {        // Construct an empty list.
    next = null;
}
public musicNode(String t) {
    track = t; next = null;
}
public musicNode(String t, musicNode ptr) {
    track = t; next = ptr;
}

 public boolean LTTrack(musicNode x) {   // Compares tracks according to alphabetical order on strings
     if (this.track.compareTo(x.track)<=0) return true;
     else return false;
 }

 public boolean LTPlayed(musicNode x) {   // Compares according to the played field.
     if (this.played <= x.played) return true;
     else return false;
 }

 public boolean LTTag(musicNode x) {   // Compares according to the shuffle tag.
     if (this.shuffleTag <= x.shuffleTag) return true;
     else return false;
 }

};


public class MusicPlayer {
    protected musicNode head = null; // Pointer to the top of the list.
    int length=0;   // the number of nodes in the list. 

void sortTrack() {
    musicNode main = this.head;
    mergeSort(main);
}

public musicNode mergeSort(musicNode a) {
    if (a == null || a.next == null)
        return a;

    musicNode nodeLeft = a;
    musicNode nodeRight = a.next;

    while((nodeRight != null) && (nodeRight.next != null)){
        a = a.next;
        nodeRight = (nodeRight.next).next;
    }
    nodeRight = a.next;
    a.next = null;

    return merge(mergeSort(nodeLeft), mergeSort(nodeRight));
}

public musicNode merge(musicNode leftStart, musicNode rightStart){
    musicNode temp = new musicNode();
    musicNode head = temp;
    musicNode c = head;
    while ((leftStart != null) && (rightStart != null)) 
    {
        if (leftStart.track.compareTo(rightStart.track) <= 0){
            c.next = leftStart;
            c = leftStart;
            leftStart = leftStart.next;
        } else {
            c.next = rightStart;
            c = rightStart;
            rightStart = rightStart.next;
        }
    }
    c.next = (leftStart == null) ? rightStart : leftStart;
    return head.next;
 }

这是为了测试目的而在其上运行的jUnit测试

@Test
public void testSortMixed() {   
    MusicPlayer trackList= new MusicPlayer();
    trackList.insertTrack("d");
    trackList.insertTrack("b");
    trackList.insertTrack("e");
    trackList.insertTrack("a");
    trackList.insertTrack("c");

    MusicPlayer trackListTwo= new MusicPlayer();
    trackListTwo.insertTrack("e");
    trackListTwo.insertTrack("d");
    trackListTwo.insertTrack("c");
    trackListTwo.insertTrack("b");
    trackListTwo.insertTrack("a");

    trackList.sortTrack();
    musicNode tmp= trackList.head;
    musicNode tmp2= trackListTwo.head;
    for(int i=0; i< 5; i++){
        assertEquals(tmp2.track, tmp.track);
        tmp2= tmp2.next;
        tmp=tmp.next;
    }
}    

0 个答案:

没有答案