This is part of my linked list program.
I need to change the data(node) with a new one when it is found (using the find function. And in other case I need to delete the found item using deleteNode() function, once the matching item is found.
But, it is not happening. I have been playing with it for hours now, still cannot figure it out. Any ideas?
//for finding the item + sorting the linked list
template <class DataType>
bool List<DataType>::find(DataType item, Node<DataType>* &ptrPrev){
bool found = false;
Node<DataType> *ptr = start;
while((ptr != NULL)&&(item > ptr->info)&&(!found)){
if(item > ptr->info){
ptrPrev = ptr;//step 1
ptr = ptr->next;//step 2
}else
found = true;
}
return found;
}
//once the right movie is found, then it's info is updated by this function.
template <class DataType>
bool List<DataType>::updateData(DataType &newItem){
bool found = false;
Node<DataType> *ptr = start;
Node<DataType> *prevNode = NULL;
found = find(newItem, prevNode);
if(found){
if(prevNode == NULL){
ptr = start;
ptr->info = newItem;
}else{
prevNode->next->info = newItem;
}found = true;
}
return found;
}
//it deletes the found item from the linked list.
template <class DataType>
bool List<DataType>::deleteNode(DataType& item){
bool found = false;
Node<DataType> *ptr = start;
Node<DataType> *ptrPrev = NULL;
found = find(item, ptrPrev);
if(found){
if(ptrPrev != NULL){
ptr = ptrPrev->next;
ptrPrev->next = ptr->next;
}else{
ptr = start;
start = ptr->next;
}
delete ptr;
numItems--;
}
return found;
}