Hello all...
I was looking around the forum for other similar problems but I didn't find anything so I decided to post.
I am creating a
Linear linked list in c++. I've written all the functions for it, but now I want to try and do them using recursion.
I managed to make functions for
adding and
displaying nodes on the list, but I am stuck on the function for
removing nodes. First I want to try and write a function the removes the last item on the list. But for some reason my code isn't working properly.
Here is the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void removeLastItem(node * &head)
{
if(!head)
{
delete head;
head = NULL;
cout << "\nItem Deleted\n";
return;
}
if(head)
{
cout << "\nAdvancing item...";
removeLastItem(head->next);
return;
}
}
|
NODE - My structure name
NEXT - The pointer to next element.
HEAD - The first (head) pointer.
** The couts in the if statements are just for testing. In fact after I Run my program it does as it is supposed - enters the second
if /b]case as many times as there are elements and then executes the first [b]if statement. But for some reason it does not delete the actual node.
Any help is much appreciated