簡體   English   中英

使用linkedList無法正常工作的mergesort - 用Java實現

[英]mergesort using linkedList not working correctly - implemented in Java

我已經為自定義LinkedIntList實現了mergesort方法,它只是一個LinkedList類。 我很難理解它失敗的原因。 那就是它不會產生任何結果。 我多次通過代碼,我無法弄清楚。

這是代碼:

class ListNode {
 public int data;       // data stored in this node
 public ListNode next;  // link to next node in the list

 // post: constructs a node with data 0 and null link
 public ListNode() {
     this(0, null);
 }

 // post: constructs a node with given data and null link
 public ListNode(int data) {
     this(data, null);
 }

 // post: constructs a node with given data and given link
 public ListNode(int data, ListNode next) {
     this.data = data;
     this.next = next;
 }
}

LinkedIntList類:

public class LinkedIntList {
 public ListNode front;

 // Constructs an empty list.
 public LinkedIntList() {
     this(null);
 }

 public LinkedIntList(ListNode node){
     front=node;
 }

 public String toString() {
 if (front == null) {
     return "[]";
 } else {
     String result = "[" + front.data;
     ListNode current = front.next;
     while (current != null) {
         result += ", " + current.data;
         current = current.next;
     }
     result += "]";
     return result;
 }


  public void sort(){
     front=mergeSort(front); 
 }

    public ListNode mergeSort(ListNode node) {
        //ystem.out.println("merge sort is called");
        //first get middle of linkedlist, using two pointers
        //you can also get this by size/2

        //step 1: get middle pointers
        if (node==null || node.next==null)
            return null;
        ListNode runner=node.next.next;
        ListNode walker=node;
        while(runner!=null && runner.next!=null){
            runner=runner.next.next;
            walker=walker.next;
        }

        //At this point walker is in center
        ListNode right=walker.next;
        walker.next=null;
        ListNode left=node;
        left=mergeSort(left);
        right=mergeSort(right);

        node=merge(left,right);

        return node;
    }

    //merge of two linkedlist happens here
    private ListNode merge(ListNode l1, ListNode l2) {

        if (l1==null){
            return l2;
        }
        if (l2==null){
            return l1;
        }

        ListNode head=null;
        ListNode curr=null;
        ListNode temp=null;

        while(l1!=null && l2!=null){
            temp=(l1.data < l2.data ? l1 : l2);
            if (head==null){
                head=temp;
                curr=head;
            }
            else {
                curr.next=temp;
                curr=curr.next;
            }
            if (temp==l1){
                l1=l1.next;
            }
            else l2=l2.next;
        }
        if (l1!=null){
            curr.next=l1;
        }
        if (l2!=null){
            curr.next=l2;
        }

        return head;
    }
}

測試這個的實用程序類:

public class UtilityMain {

     public static void main(String[] args){

         ListNode node5 = new ListNode(1,null);
         ListNode node4=new ListNode(2,node5);
         ListNode node3=new ListNode(3,node4);
         ListNode node2=new ListNode(4,node3);
         ListNode node1 = new ListNode(5,node2);

         LinkedIntList l = new LinkedIntList(node1);
         System.out.println("before sorting " + l);
             l.sort();
         System.out.println("Afer sorting " + l);

     }
}

輸出:--->

before sorting [5, 4, 3, 2, 1]
             After sorting []

至少部分問題在於:

if (node==null || node.next==null)
    return null;

如果當前節點為null,則應返回null,但是,如果node.next為null,則應返回node。

考慮列表的情況[3,2]

列表節點是(NODE [3],Next - > NODE [2],Next - > [NULL])

你用Walker =(NODE [3],Next - > [NULL])和Runner =(NODE [2],Next - > [NULL])打破列表

然后在Walker(重命名為Left)和Runner(重命名為Right)上調用合並排序

在每個調用中,Node.next測試為null,因此它返回null而不是您想要的列表,這應該是傳入的列表。

有一個錯誤會在合並方法中生成一個NULL指針異常。你能找到嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM