insert a node after an iter postion in a deque class

Hoang Minh

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++;
        }
    }   
}
West

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ListView postion in list after scrolling

Is it possible to insert column in the first postion with sas/sql

How to insert a node after a selection?

Xpath, find postion of a node inside html code

After Scrolling, Recycler moves to first postion

Recyclerview item postion NOT_FOUND after scrolling

Jquery scroll to new postion when class changes

Complete Class Deque

Class with iter but not next

Class using the iter protocol

Insert after x node minidom xml python

Insert node at specified custom class iterator position

How to insert Node based on css class in JS?

insert html after all selected class javascript

How to insert a node html before and after a node in R

Insert a node in a doubly linked list after a given node

How to change the postion of the title after changing the height of Navigation Bar in Xcode

__iter__ in Class file, for loop

cython cannot insert into deque using iterators

How to modify a jquery html clone and insert multiple times after a node?

Why no "delete" after "new" in linked list insert node

Linked List Class in MATLAB - Insert node manually without insertAfter()

Bulk insert class object in sqllite database in node js

delete the hashed node in deque in O(1)

Insert elements after each image with a specific class using plain javascript

Multiple elements with same class - only insert after last one

How to insert row after last row with a certain class with JQuery?

Insert HTML element after a specific div class name with Typescript & Angular

how to insert the lastInsertId in my custom database class after retrieving