合并排序与链接列表

时间:2019-02-07 20:36:04

标签: java merge linked-list mergesort

我正在尝试为我的计算机科学课程创建一个程序,该程序采用一个链表并通过合并排序算法对其进行排序。我在获取链表和通过找到中间节点递归拆分节点时遇到困难。我有三个类,一个用于节点,列表和合并排序类。

static Mynode h;
static Mylist list = new Mylist();

public static void Sort(String[] list){
}

public static void main(String[] args){
    /*
    node sort(node h);
    node l2 = split(h);
    */
    list.insert("cat");
    list.insert("ok");
            list.insert("ok");

    list.printdata();
    split(list);
}

public void sort(){
    String head;
}
public static void merge(Mylist list, int right, int left){

}
public static void split(Mylist list){
    Mynode tempFast = h;
    Mynode tempslow = h;        
    Iterator iterator = tempFast.iterator();
    Iterator iterator = tempslow.iterator();

        while(tempslow.getNext()!=null && tempslow.getNext()!=null){
            tempFast.getNext();
            tempFast.getNext();
            tempslow.getNext();
        }
}

public Merge(String data){
    /*
    this.data = data; 
    */
}

/*
public Node msort(String r){

}
    /*
    node l2 = split(h);
    h = msort(h);
    l2 = msort(l2);
    return merge(h, l2);
    */
}

// input: java myms < word3
// output: Unsorted: cow, zebra, ant || Sorted: ant, cat, zebra

static Mynode h;
public Mylist(){
    this.h = null;
}

public void insert(String s) {
//method for node insertion
   this.h = new Mynode(s, this.h);
}
public boolean contains(String s){
//method for identifying contains specific data
    Mynode temp = this.h;
    while(temp !=null){
    //while the node contains a value
        if(s.equals(temp.getData())){
        //if the node contains the specified data, return true
            return true;
        }
        temp = temp.getNext();
        //set the node reference to the next value
    }
    return false;
}
public void printdata(){
    Mynode temp = this.h;
    while(temp !=null){
        System.out.println(temp.getData());
        //print the current node, then increment the temp node one over
        temp = temp.getNext();
    }
}
public void deletion (String s){
//method for deletion
    Mynode temp = this.h;
    Mynode previous = null;
    if (temp != null && temp.getData().equals(s)) {
    //when node contains data and the specified data matched the node in the linked list 
        this.h = temp.getNext();
        //node reference is set to the next value
        return; 
    } 
    while (temp != null && !(temp.getData().equals(s))) { 
    //retrieve node reference for next value
        previous = temp; 
        temp = temp.getNext(); 
        //set node to the next value
    } 
    if(temp ==null) {
        return;
    }
    previous.setNext((temp.getNext())); 

}
public Iterator iterator() {
    return new Myiter(this.h);
}
private class Myiter implements Iterator {

    private Mynode temp;

    public Myiter(Mynode temp) {
        this.temp = h;
    }
    public boolean hasNext() {
    //checks if there is an element next in the list
        return this.temp != null;
    }
    public Object next() {
    //gets the next element in the list
        Object returndata = this.temp.getData();
        temp = temp.getNext();
        return  returndata;
    }
    public void remove() {
    //not implemented
    }
}
}

public class Mynode {
private String data;
private Mynode next;
public Mynode (String d, Mynode n){
    this.data = d;
    this.next =n;
}
public String getData(){
    return this.data;
}
public void setNext(Mynode n){
    this.next = n;
}
public Mynode getNext(){
    return this.next;
}

}

1 个答案:

答案 0 :(得分:0)

如果还为我们提供了Mynode类,则调试起来可能会更容易。

从我们现在得到的信息来看,似乎split方法将是无限的外观。

LinkedList中的getNext方法不应更改当前对象中的任何内容,而应仅是列表中下一个节点的吸气剂。因为您正在调用getNext但不存储以任何方式返回的Mynode,所以getNext方法将返回相同的节点,因为“ tempFast”和“ tempSlow”从不引用列表中的下一个Mynode。

代替此处的代码:

    while(tempslow.getNext()!=null && tempslow.getNext()!=null){
        tempFast.getNext();
        tempFast.getNext();
        tempslow.getNext();
    }

我建议将tempFast和tempSlow变量设置为列表中的下一个节点,如下所示:

    while(tempslow.getNext()!=null && tempslow.getNext()!=null){
        tempFast = tempFast.getNext();
        if(tempFast != null){
            tempFast = tempFast.getNext();
        }
        tempSlow = tempslow.getNext();
    }