合并在Java中以递归方式对链接列表进行合并

时间:2014-01-14 00:48:27

标签: java linked-list mergesort singly-linked-list

因此,任务是实现对链表进行排序的链表和合并排序。我充分意识到在工业中我很可能不必实现任何这些,但我觉得这是练习Java的好方法。以下是我到目前为止所做的事情:

节点类:

public class Node<E extends Comparable<E>>
    {

    public E data;
    public Node<E> next;

    public Node(E data)
    {
        this.data = data;
        next = null;
    }

    public void printData()
    {
        System.out.print(data + " ");
    }
}

LinkedList类:

public class LinkedList<E extends Comparable<E>>
{

    protected Node<E> root;
    protected int size = 0;

    public LinkedList()
    {
        root = null;
    }


    public void addBeg(E e)
    {

        Node<E> newNode = new Node<E>(e);
        newNode.next = root;
        root = newNode;

        size++;
    }

    public Node deleteBeg()
    {
        Node<E> temp = root;
        if(!isEmpty())
        {
            root = root.next; 
            size--;
        }
        return temp;
    }

    public void setRoot(Node<E> newRoot)
    {
        root = newRoot;
    }

    public boolean isEmpty()
    {
        return root == null;
    }

    public Node<E> getRoot()
    {
        return root;
    }

    public void printList()
    {
        Node<E> cur = root;   
        while(cur!=null)
            {
                cur.printData();
                cur=cur.next;
            }
        System.out.println();        
    }
}

MergeSorter类:

public class MergeSorter<E extends Comparable<E>>
{

    public MergeSorter()
    {

    }

    private void split(LinkedList<E> list, LinkedList<E> firHalf, LinkedList<E> secHalf)
    {
        //if 0 or only 1 elements in the list - it doesn't seem to work, however
        if(list.getRoot() == null || list.getRoot().next == null)firHalf = list;
        else{
            Node<E> slow = list.getRoot(); 
            Node<E> fast = list.getRoot().next; 
            while(fast!=null)
            {
                fast = fast.next;
                if(fast!=null)
                {
                    fast = fast.next;
                    slow = slow.next;
                } 
            }
            //If I use the following line firHalf list is empty when in the caller of this method (it's not in this method, however). Don't understand why ):
            //firHalf = list; 
            firHalf.setRoot(list.getRoot());
            secHalf.setRoot(slow.next);
            slow.next = null;
        }

    }



    private LinkedList<E> merge(LinkedList<E> a, LinkedList<E> b)
     {
        LinkedList<E> mergedList = new LinkedList<E>();    
        Node<E> dummy = new Node<E>(null);
        Node<E> tail = dummy;

        while(true)
        {         
            if(a.getRoot() == null){
                tail.next = b.getRoot();
                break;
            }
            else if(b.getRoot() == null){
                tail.next = a.getRoot();
                break;
            }

            else 
            {
                if(a.getRoot().data.compareTo(b.getRoot().data) <= 0)
                {
                    tail.next = a.getRoot();
                    tail = tail.next;
                    a.setRoot(a.getRoot().next);
                }

                else
                {   
                    tail.next = b.getRoot();
                    tail = tail.next;
                    b.setRoot(b.getRoot().next);
                }
                tail.next = null;
            }

        }
        mergedList.setRoot(dummy.next);
        return mergedList;
    }

    public void mergeSort(LinkedList<E> list)
    {
        Node<E> root = list.getRoot();
        LinkedList<E> left  = new LinkedList<E>();
        LinkedList<E> right = new LinkedList<E>();

        if(root == null || root.next == null) return; //base case
        split(list, left, right); //split

        mergeSort(left);
        mergeSort(right);

        list = merge(left, right); // when this mergeSort returns this list should be  
                                   // referenced by the left or right variable of the 
                                   // current mergeSort call (but it isn't!)
    }
}

我是Java的新手(来自C背景)所以如果我的代码完全错误,我会提前表示非常抱歉。当我独立测试MergeSorter类中的拆分和合并方法时,一切似乎都有效(拆分包含0或1元素的列表不起作用并且让我发疯,但这不是合并排序所必需的)。但是,mergeSort方法不起作用,我似乎无法弄明白。我试图自己调试它,当两个半部合并到一个列表然后递归返回时似乎有问题。新合并的列表应该由当前mergeSort调用的左或右变量引用,而是仅获取最后一个元素而不是整个列表。

1 个答案:

答案 0 :(得分:0)

Java中的方法参数总是按值传递。

这可能有点令人困惑,因为对象总是通过引用访问,所以你可能会认为它们是通过引用传递的;但他们不是。相反,引用按值传递。

这意味着什么,这样的方法:

public void methodThatDoesNothing(Object dst, Object src) {
    src = dst;
}

实际上什么也没做。它修改其局部变量src以引用与局部变量dst相同的对象,但这些只是在函数返回时消失的局部变量。它们完全独立于传递给方法的任何变量或表达式。

所以,在你的代码中,这个:

firHalf = list;

并没有真正做任何事情。我想你想要的是:

while (! firHalf.isEmpty()) {
    firHalf.deleteBeg();
}
if (! list.isEmpty()) {
    firHalf.addBeg(list.root().data);
}

修改了firHalf引用的被反对者,因此它与list具有相同的零或一元素。

相关问题