0

This is the section that messes up. It's meant to de-reference _head->prev to a nullptr but if there is no prev it crashes so I'm attempting to handle that crash. When it gets to

if( _head )
   (!_head->prev)

It messes up as there is no previous. I cannot figure out how to write an if statement to handle this moment so that it wont break. If there is no prev I just want it to --_size delete tmpnode and return result.

template <class list>
list linked_list<list>::pop_front()
{
assert( !empty() );

node_ptr_t tmpNode(_head);
list result = _head->data;
if(_head->next)
    _head = _head->next;
else
    _head = _tail;


if ( _head ) 
    if(!_head->prev)
    {
        --_size;
        delete tmpNode;
        return result;
    }else{
    _head->prev = nullptr;
    }else {
    _tail = nullptr;
}
//_head ? _head->prev=nullptr : _tail=nullptr;

--_size;
delete tmpNode;
return result;

}

J-Americano
  • 188
  • 1
  • 10
  • what its supposed to do – karan Feb 20 '14 at 04:30
  • It's meant to dereference _head->prev – J-Americano Feb 20 '14 at 04:33
  • compare _head->prev==NULL instead of what u did – karan Feb 20 '14 at 04:36
  • Doesn't work as prev isn't null. It isn't there. I'm pointing to a location where there is nothing but I can't think of how to check if it's there before pointing to it. – J-Americano Feb 20 '14 at 04:47
  • When you created `prev` and `next`, it does not appear that you set their initial values to null, so they contain garbage values. Try setting them to null and run your tests again. – OnlineCop Feb 20 '14 at 04:57
  • If it is not null, but also not a valid pointer, then there is no reliable way to check it. It should either be null or valid, nothing else. Which means you are not managing the nodes correctly. Is there a reason why you are not using `std::list` instead? – Remy Lebeau Feb 20 '14 at 04:57
  • Honestly. Because this is a project in which I'm suppose to come up with a SquareList which is a linked list data container of linked lists. I feel if I wanted to know C++ it would benefit me. I want to work on databases. Not with Boost. Prev and Next can't be set to null. This is an issue with my copy ctors not this code. Apparently something there is messed up. It's not copying right as my original list runs through this code no problem. My copy does not. – J-Americano Feb 20 '14 at 05:28

0 Answers0