But after entering the number to be deleted, the program runs forever.
Here is the output before the deleteNode function is called:
Enter integers ending with -999
65 34 23 90 87 -999
List 1: 23 34 65 87 90
List 2: 23 34 65 87 90
Enter the number to be deleted:
When I delete the first node in the list, the node get deleted and everything is fine.
But when I delete the last node, the program bombs out.
And when I delete any node in the middle of the list, the program runs for ever giving this output:
5701824 5710728 5710424 5701824 5710728 5710424 5701824 5710728 571042
5701824 5710728 5710424 5701824 5710728 5710424 5701824 5710728 57104
4 5701824 5710728 5710424 5701824 5710728 5710424 5701824 5710728 5710
24 5701824 5710728 5710424 5701824 5710728 5710424 5701824 5710728 571
424 5701824 5710728 5710424 5701824 5710728 5710424 5701824 5710728 57
Process returned 255 (0xFF) execution time : 22.198 s
Press any key to continue.
The code is too long so I have posted the deleteNode function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
template<class Type>
void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if(first == NULL)
cerr<<"Cannot delete from an empty list"<<endl;
else
if(first->info == deleteItem) //node to be deleted is the
//first node
{
current = first;
first = first->next;
if(first != NULL)
first->back = NULL;
else
last = NULL;
count--;
delete current;
}
else
{
found = false;
current = first;
while(current != NULL && !found) //search the list
if(current->info >= deleteItem)
found = true;
else
current = current->next;
if(current == NULL)
cout<<"The item to be deleted is not in the list"
<<endl;
else
if(current->info == deleteItem) //check for equality
{
trailCurrent = current->back;
trailCurrent->next = current->next;
if(current->next != NULL)
current->next->back = trailCurrent;
if(current == last)
last = trailCurrent;
count--;
delete current;
}
else
cout<<"The item to be deleted is not in list."
<<endl;
}//end else
}//end deleteNode
|