为什么链表上的这种合并排序不能正常工作?

阿朗佐·罗布

(免责声明:对于学校,所以不能导入其他 Java 实用程序)

所以我必须在一个链表上合并排序,而且我几乎把所有的都记下来了。这里是:

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

public musicNode() {        // Here's how we 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;
}
}; 

// This class represents a playlist;
// We assume that each track appears at most once in the playlist

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

public  MusicPlayer() {
}
public void setToNull() {
    head = null;
}
public boolean isEmpty() {
    return head == null;
}

public musicNode head() {
    return head;
}

void insertTrack(String name) { // Inserts a new track at the top of the list.
    musicNode temp= new musicNode(name, head);
    head= temp;
    length++;
}

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


public musicNode mergeSort(musicNode head) {
    if ((head == null) || (head.next == null)){
        return head;
    }
    musicNode left = head;
    musicNode right = head.next;

    while((right != null) && (right.next != null)){
        head = head.next;
        right = (right.next).next;
    }
    right = head.next;
    head.next = null;

    return merge(mergeSort(left), mergeSort(right));
}

还有这个 JUnit 测试:

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

问题是它根据您插入的最后一首曲目进行排序,并且仅从那时起。因此,假设您从 af 插入字母,但您插入的最后一个字母是“c”,它只会显示“cdef”。但是如果最后一个是“a”,那么它会按预期工作。

所以它是如何工作的,当你插入一个轨道时,它被插入到列表的开头,而不是结尾,成为头部。我觉得这可能是什么搞砸了,因为我改编并从我的笔记和在线插入底部查看。

我不知道如何解释这一点。我也知道它根据最后插入的内容进行排序(在上面的 JUnit 测试中,它排序为“cde”,因为我创建了一个 main 函数并使用它)

任何帮助表示赞赏。

命令行

关键点是 sortTrack 方法中的第二行:

void sortTrack() {
    musicNode main = this.head;
    this.head = mergeSort(main); // you forgot to set the head of linked list to the merged
}

我已经在我的笔记本电脑上测试过了,现在一切正常 xD

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章