Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 链表上的合并排序_Java - Fatal编程技术网

Java 链表上的合并排序

Java 链表上的合并排序,java,Java,我一直在尝试将合并排序应用到一个链表上,我对我正在做的事情有一个大致的了解,但是运行时的代码没有通过我的简单jUnit测试。我不确定哪一部分出了问题,似乎无法解决需要修复的问题。 任何帮助都是值得感激的,解释对未来的时代更是如此 class musicNode { String track; // The name of the track int played= 0; // The number of times played int shuffleTag= 0;

我一直在尝试将合并排序应用到一个链表上,我对我正在做的事情有一个大致的了解,但是运行时的代码没有通过我的简单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;
 }
类音乐节点{
String track;//轨迹的名称
int played=0;//播放的次数
int-shuffleTag=0;
下一步;
public musicNode(){//构造一个空列表。
next=null;
}
公共音乐节点(字符串t){
track=t;next=null;
}
公共音乐节点(字符串t,音乐节点ptr){
轨道=t;下一个=ptr;
}
公共布尔LTTrack(musicNode x){//根据字符串的字母顺序比较轨迹

if(this.track.compareTo(x.track):D我想你应该告诉我们这是否是家庭作业(如果不是JDK列表的问题)这是工作,但我花了一天的时间来研究这个问题,仍然无法得到它,所以我来这里寻求帮助。这有什么问题吗?:D我想你应该告诉我们这是否是家庭作业(如果不是JDK列表有什么问题的话)这是工作,但我花了一天的时间来做这件事,但仍然无法得到它,所以我来这里寻求帮助。这有什么问题吗?
@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;
    }
}