简体   繁体   English

Java - Doubly Linked List添加值

[英]Java - Doubly Linked List adding values

I'm trying to merge two (pre-sorted) doubly linked lists together and it keeps getting infinite loops or a one element list when trying to add. 我正在尝试将两个(预先排序的)双向链接列表合并在一起,并且在尝试添加时它会不断获得无限循环或一个元素列表。

The expected outcome of the code below should be the list: 以下代码的预期结果应该是列表:

[0,1,2,3,4,5,6,7,8,9]

So I Have : 所以我有 :

public static void main(String[] args) {
    TheLinkedList<Integer> oddList = new TheLinkedList<Integer>();
    TheLinkedList<Integer> evenList = new TheLinkedList<Integer>();

    // Test lists 
    oddList.add(new Integer(9));
    oddList.add(new Integer(7));
    oddList.add(new Integer(5));
    oddList.add(new Integer(3));
    oddList.add(new Integer(2));
    oddList.add(new Integer(1));

    evenList.add(new Integer(8));
    evenList.add(new Integer(6));
    evenList.add(new Integer(4));
    evenList.add(new Integer(2));
    evenList.add(new Integer(0));

    //System.out.println(oddList.toString());
    //System.out.println(evenList.toString());
    oddList.merge(evenList);
    //System.out.println(theList.toString());
}

Note this is in a different Class and you CANNOT accesss oddList or evenList directly 请注意,这是在不同的类中,您不能直接访问oddList或evenList

// Self explanatory getter and setter methods
public void add(T newValue) {
    head = new Node<T>(newValue, head, null);
    if (head.getNext() != null)
        head.getNext().setPrevious(head);
    else
        tail = head;
    count++;
}

public void merge(TheLinkedList<T> two) {
    do {

        if (head.getValue().compareTo(two.head.getValue()) <= 0) {
            head = head.getNext();
            continue;
        }
        if (head.getValue().compareTo(two.head.getValue()) >= 0){
            two.head = two.head.getNext();
        }
    } while (head != null && two.head != null);
}

I don't see any actual merging going on here? 我没有看到任何实际的合并在这里? Assuming the lists are in descending order you should be comparing the head of one list with the head of the other, and if the value of the other list is less than the value of your main list, you should insert that value to your main list to the next node of your head node and move on to the next value. 假设列表按降序排列,您应该将一个列表的头部与另一个列表的头部进行比较,如果另一个列表的值小于主列表的值,则应将该值插入主列表中到头节点的下一个节点,然后转到下一个值。 You will need to be careful because the node you add to your main list will need references to the next and previous items if it is to fit properly. 您需要小心,因为添加到主列表中的节点如果要正确匹配则需要引用下一个和上一个项目。

If the order is not guaranteed then to my knowledge you need to sort the list first, and then merge them to prevent having to walk through the entire referenced linked list multiple times in worst case scenarios. 如果订单不能保证,那么据我所知,您需要先对列表进行排序,然后合并它们以防止在最坏的情况下多次遍历整个引用的链表。

Edit: here's a code example. 编辑:这是一个代码示例。 You could probably also do it with recursion, but recursion makes my head hurt so... 你可能也可以通过递归来做,但递归让我的头受伤所以......

public void merge(TheLinkedList<T> two) {
Node workingNodeOnOne = this.head;
Node workingNodeOnTwo = two.head;

while (workingNodeOnTwo != null)
        if (workingNodeOnOne.getValue().compareTo(workingNodeOnTwo.getValue()) < 0) {
            //this is if the value of your second working node is greater than the value of your first working node.
            //add the two.head.getValue() value of your current list here before the first working node...
            this.addBefore(workingNodeOnTwo.getValue(), workingNodeOnOne) 
            //note that this does change the value of this.head, but it doesn't matter as we are sorted desc anyways

            //given the constraints of what you have presented, you should never even hit this code block
            workingNodeOnTwo = workingNodeOnTwo.next();
        }
        else { //this is if the head value of second list is less than or equal to current head value, so just insert it after the current value here

            this.add(workingNodeOnTwo.getValue(), workingNodeOnOne); //insert the value of the second working node after our current working node
            workingNodeOnOne = workingNodeOnOne.next();
            workingNodeOnTwo = workingNodeOnTwo.next(); //go on to the next nodes
            }

}

This is definitely gonna need some tweaking depending on your implementation but the logic is sound, I believe. 根据你的实现,这肯定需要一些调整,但我认为逻辑是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM