Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我用while在两个双链表中循环?_Java_While Loop_Linked List_Doubly Linked List - Fatal编程技术网

Java 为什么可以';我用while在两个双链表中循环?

Java 为什么可以';我用while在两个双链表中循环?,java,while-loop,linked-list,doubly-linked-list,Java,While Loop,Linked List,Doubly Linked List,好的,任务是: 给定2个整数列表,按照以下规则将它们合并为一个: 交替添加两个列表中的所有偶数(列表1从第一个开始,列表2从最后一个开始) 将列表1中剩余的所有偶数相加 将列表2中所有剩余的偶数相加 从第一个开始添加列表1中的所有奇数 从最后一个开始添加列表2中的所有奇数 所以对于前男友来说 清单1:1234456 清单2:78910 清单3应该是:1028844613597 但是,我的函数返回244681013579 以下是我编写的函数: public static void merge(DL

好的,任务是:

给定2个整数列表,按照以下规则将它们合并为一个:

  • 交替添加两个列表中的所有偶数(列表1从第一个开始,列表2从最后一个开始)

  • 将列表1中剩余的所有偶数相加

  • 将列表2中所有剩余的偶数相加

  • 从第一个开始添加列表1中的所有奇数

  • 从最后一个开始添加列表2中的所有奇数

  • 所以对于前男友来说

    清单1:1234456 清单2:78910

    清单3应该是:1028844613597

    但是,我的函数返回244681013579

    以下是我编写的函数:

    public static void merge(DLL<Integer> list1 , DLL<Integer> list2, DLL<Integer> list3) {
    
        DLLNode<Integer> curr1=list1.getFirst();
        DLLNode<Integer> curr2=list2.getLast();
    
        while (curr1!=null && curr2!=null) {
    
            if (curr1.element%2==0) list3.insertLast(curr1.element);
            curr1=curr1.succ;
    
            if (curr2.element%2==0) list3.insertLast(curr2.element);
            curr2=curr2.pred;
        }
    
        if (curr1!=null) {
            while (curr1!=null) {
                if (curr1.element%2==0)
                    list3.insertLast(curr1.element);
                curr1=curr1.succ;
            }
        }
    
        if (curr2!=null) {
            while (curr2!=null) {
                if (curr2.element%2==0)
                    list3.insertLast(curr2.element);
                curr2=curr2.pred;
            }
        }
    
        curr1=list1.getFirst();
        while (curr1!=null) {
            if (curr1.element%2!=0)
                list3.insertLast(curr1.element);
            curr1=curr1.succ;
        }
    
        curr2=list2.getLast();
        while (curr2!=null) {
            if (curr2.element%2!=0)
                lista.insertLast(curr2.element);
            curr2=curr2.pred;
        }
    
    
    
    }
    
    公共静态无效合并(DLL列表1、DLL列表2、DLL列表3){
    DLLNode curr1=list1.getFirst();
    DLLNode curr2=list2.getLast();
    while(curr1!=null&&curr2!=null){
    if(curr1.element%2==0)list3.insertLast(curr1.element);
    curr1=curr1.succ;
    if(curr2.element%2==0)list3.insertLast(curr2.element);
    curr2=curr2.pred;
    }
    如果(curr1!=null){
    while(curr1!=null){
    if(curr1.element%2==0)
    清单3.insertLast(curr1.element);
    curr1=curr1.succ;
    }
    }
    如果(curr2!=null){
    while(curr2!=null){
    if(curr2.element%2==0)
    列表3.insertLast(curr2.element);
    curr2=curr2.pred;
    }
    }
    curr1=list1.getFirst();
    while(curr1!=null){
    if(当前元素%2!=0)
    清单3.insertLast(curr1.element);
    curr1=curr1.succ;
    }
    curr2=list2.getLast();
    while(curr2!=null){
    if(curr2.元素%2!=0)
    lista.insertLast(curr2.element);
    curr2=curr2.pred;
    }
    }
    

    不知怎的,它没有进入第一个while循环。这可能是什么原因

    您没有互换地添加偶数元素。如果输出列表的第一个元素应该是第一个列表的偶数元素,则必须迭代第一个列表,直到找到该列表上的第一个偶数元素。然后,您应该开始迭代第二个列表

    您可以使用一个标志,告诉您下一个偶数元素应该从哪个列表中获取:

    boolean takeFirst = true;
    while (curr1!=null && curr2!=null) {
        if (takeFirst) { // next even element should come from the first list
            if (curr1.element%2==0) {
                list3.insertLast(curr1.element);
                takeFirst = false;
            }
            curr1=curr1.succ;
        }
        if (!takeFirst) { // next even element should come from the second list
            if (curr2.element%2==0) {
                list3.insertLast(curr2.element);
                takeFirst = true;
            }
            curr2=curr2.pred;
        }
    }
    

    你试过调试它吗?如果它在-循环时没有进入第一个
    ,那么可能是
    DLL
    类方法
    getFirst()
    getLast()
    有问题?@AmitKhandelwal我做了,它没有显示任何内容。@kiltek我先检查了它们,它们正在按应有的方式工作。