0

I'm trying to build a function that is gonna transfer all elements from one list to another in deque class. For example, if you have list 1 {1,2,3} and list 2 {4,5}. What I am going to do is transfer all the elements inside list 2 into list 1, make it list 1 {1,2,3,4,5} (ordered is not required) and list 2 will be empty after that. However, the result list that I'm getting is not correct at all....Can anyone what's wrong with my code please ? Thank you

void meld(Deque<E>& other)
{

    DNode<E>* a = _head;
    DNode<E>* b = _tail;
    DNode<E>* c = other.get_head();
    DNode<E>* d = other.get_tail();
    DNode<E>* temp = c;
    b->set_next(temp);
    temp->set_prev(b);
    _size += other.size();
    c = nullptr;            
}
4

1 回答 1

1

您可能需要将 的内容设置other为空。您发布的代码仅设置c为 null ,但c无论如何都是一个即将超出范围的局部变量。你可能需要改变other._head = 0

请注意,您的代码不必要地设置ad. 同样,temp也不是真的需要:您可以c改为使用。

于 2013-11-02T01:22:07.297 回答