简体   繁体   中英

Swapping lists trouble with last value, c++

I am trying to swap the values between two lists, it uses a member function of a custom List class i created called ListNum which is friends with a node class and an iterator class. I am able to swap them but I keep getting a weird error I am not sure how to solve. On the last value for the second list I get an error when I try to implement it.

I simply insert after the first iterator for both the lists and then delete that element, the iterator automatically goes to the next value of the list after it deletes. On the last erase call it checks to see if the value being deleted is "last". On the second list the value is "last" but when it checks in the if statement of "erase" this call fails and I get a BAD_ACCESS Error. Basically trying to assign a null pointer that results in the error

The value is 6 and it is last but fails the if statement for last. I am not sure if this has to do that the first list I needed to create a pointer to this while the second list was passed as reference.

Here is my code.

int main(){

    list1.push_back(1);
    list1.push_back(2);
    list1.push_back(3);

    list2.push_back(4);
    list2.push_back(5);
    list2.push_back(6);

    list1.swap(list2);

 return 1;
}   

This is the swap member function that I am trying to implement

void ListNum::swap(ListNum& list2){

    ListNum* list1 = this;

    IteratorNum end1 = list1->end();
    IteratorNum end2 = list2.end();

    IteratorNum start1 = list1->begin();
    IteratorNum start2 = list2.begin();

    while(!start1.equals(end1) && !start2.equals(end2)){

        list1->insert(start1, start2.get());
        list2.insert(start2, start1.get());
        start1 = erase(start1);
        start2 = erase(start2);
    }  
}

The code fails on the last run of the while Loop here in the if statement (remove == last)

IteratorNum ListNum::erase(IteratorNum iter){

    assert(iter.position != NULL); //check that erase is not null

    NodeNum *remove = iter.position;
    NodeNum *after = remove->next;
    NodeNum *before = remove->previous;

    if(remove == first)
        first = after;
    else
        before->next = after;

//FAILS HERE, 6 is last int value but it doesn't pass this test. List 1 passes but list 2 does not...
    if(remove == last) //erasing at end of ListNum
        last = before;
    else
        after->previous = before;

    delete remove;

    IteratorNum r;
    r.position = after;
    r.container = this;
    return r;

}

First of all: don't reinvent the wheel. std::list is well tested and already implements std::swap() . Writing your own list is just useful for education purpose.

Anyway, don't swap the elements themself, but the pointers to them. Your list probably contains a pointer to the first and the last node. Swap these pointers, and maybe some metadate, like size, and you are done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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