【问题标题】:Why isnt this merge sort on linked list working properly?为什么链表上的这种合并排序不能正常工作?
【发布时间】:2017-04-21 13:18:28
【问题描述】:

(免责声明:用于学校,因此无法导入其他 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;
    }
}

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

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

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

任何帮助表示赞赏。

【问题讨论】:

  • 请在问题中发布相关代码,而不是在链接中。
  • @AndyTurner 抱歉,认为它会节省空间。贴出代码,带走链接
  • 不相关,但按照惯例,类名以大写字母开头。 musicNode 应该是 MusicNode

标签: java linked-list mergesort


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2019-07-31
    • 2016-11-06
    相关资源
    最近更新 更多