質問

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;            
}
役に立ちましたか?

解決

You probably need to set the content of other to be empty. The code you posted merely sets c to null but c is a local variable about to go out of scope anyway. You probably need to change other._head = 0.

Note that your code unnecessarily sets up a and d. Likewise, temp isn't really needed: you could just use c instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top