質問

I'm having problem to insert a node right after an inter position, keep having the un-handled exception, even though I check all the links to the pointers and they are all correct. Would anyone take a look at my code and see what's going wrong please ?

void insert_after(DequeIterator<E>& iter, E x)
{
    if(_size == 0)
    {
        insert_front(x);
    }
    else
    {
        assert(!iter.at_end());

        // need to check, if its the iter is the last element
        // do insert_back instead
        if(iter.node() == _tail)
            insert_back(x);
        else
        {
            // create a temp2 pointer to hold the address of iter
            DNode<E>* temp2 = iter.node();
            // set temp2 equal to the address of the node after
            // iter before inserting
            iter.node()->next()->set_next(temp2);
            // create a new node to insert
            DNode<E>* temp = new DNode<E>(iter.node(), x, temp2);
            // set the iter->next to hold the address of the new node
            iter.node()->next()->set_next(temp);
            //set the address of temp2->prev to hold the address
            // of the new node.
            // This is also where I got the exception error !!!!
            temp2->prev()->set_prev(temp);
            _size++;
        }
    }   
}
役に立ちましたか?

解決

Just my general thoughts. You actually aren't doing any iteration on the node so why do you need an iterator here? References or pointers would do just as well. I would do initialize my nodes like

DNode<E>* temp = new DNode<E>(iter.node(), x, iter.node()->next());
// Initialize new node inbetween previous nodes
temp->prev()->set_next(temp); temp->next()->set_prev(temp);
// Cut ties between nodes to avoid cycles. 

Assuming left is prev and right is next. Then all you have to do is set next on iter. Also your variables kind of put you on the spot in terms of remembering what they are. It looks like in this code you are taking the current node and then saying to the node after it I'm after you. You then initialize the new node with the current node both before and after it. You then take the new node go to the node before it which should be the current node and set the current node as the previous node. This is assuming that the functions do what my mind thinks they would do for a doubly linked list which is apparently what you have going on.

他のヒント

Suggested solution

You have the sequence a, c, with iter pointing at a, and you want a, b, c with value x stored in that new node b, right? And every node has a prev and a next pointer, which can be read using the methods of those names, and set with the setter methods, right? Then try this:

DNode<E>* a = iter.node(), c = a->next(); // both ends of the insert location
DNode<E>* b = new DNode<E>(a, x, c);      // assuming constructor sets pointers
a.set_next(b);
c.set_prev(b);

Problem analysis

Now why does your code fail?

iter.node()->next()->set_next(temp2);

Why are you modifying the next pointer of c? That should remain untouched!

iter.node()->next()->set_next(temp);

Same problem. Plus this appears to overwrite the previous call, so that one seems pointless.

temp2->prev()->set_prev(temp);

And now you are modifying the prev pointer of the node before a? Even worse.

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