//you will delete temp
temp -> b = temp -> b -> b; //so, why bother adjusting its links?
temp -> f = temp -> f -> f;
m_size--;
delete temp;
You need to adjust the links of the cell before and after you (provided that they exist). You better encapsulate that behaviour in another method List::erase( Node*)
Also, your algorithm is inefficient. Why do you go back to the beginning after deleting one instance? You already know that all that path is clear.
Instead of using that flags, you could simply return or break
1 2
if(temp -> b == NULL)
done = true; //done, but not complete, so the inner loop will keep going
yeah like ne555 said, you are updating the links for temp, but you are deleting temp, creating a hole in your list.
it might make more sense to use a node and call it 'next' and 'previous' take these nodes along with you on your search. then updating links is 'easier'. so if temp is sitting on the node to delete,
previous -> f = temp -> f;
next -> b = temp -> b;
so the node after temp links back to the one before temp and the node before temp links to the one after it.
also, drawing a picture and visualizing what you need it to do helps a lot.
This is my code for a single linked list. You should easily be able to adapt it to your doubly :)
It can be either, it is a matter of preference, my teacher gave me the .h file anyway. But I have changed my code and it still doesn't work. It keeps segmentation faulting.
On line 23, I have a suspicion you have too many ->b statements because you have already reassigned temp->b to point to the node before/in the b-direction of the node you are deleting.
This will traverse your list and stop when it finds the node to delete or hits the end of the list. the while loop should only be one line. once the node is found you want to exit the while loop. also depending on what kind of field 'data' is and what direction you are going you either want to use >= or <= while searching for the node.
while(temp -> b != NULL && temp -> data >= x) temp = temp -> b;
next you need to double check the node is the correct node to delete, if not move on or output a message.
if(temp -> data == x)
{
//update links here
Node<generic>* next = temp -> b;
Node<generic>* previous = temp -> f;
previous -> b = temp -> b;
next -> f = temp -> f;
delete temp;
m_size--;
}
else
do whatever... exit, output a message.. so on
I am not sure what your pop_front and back function are, nor do i want to know.. but what I wrote should work, as long as the rest of it is working properly
template<typename generic>
void List<generic>::remove(generic x)
{
//pointers set to 0 zero in constructor
if(m_front == 0){
//list is empty
return;
}else{
if(m_front -> data == x){
//First node contains data
Node<generic>* tmp = m_front; // create a tmp pointer to front
m_front = m_front -> b; // move front pointer to next element
delete tmp; // delete front element
return;
}else{
//First was not what we looked for, iterate over collection
if(m_front -> b == 0){
//No more elements in list
return;
}
while(){
// Do something here , I leave that to you =)
}
}
}
}