I'm trying to delete the nodes that are even in a doubly linked list by recursion. The problem is that it deletes the nodes but still show garbage in the deleted nodes space.
This is the output:
Here is the original list: 2 -> 2 -> 23 -> 2 -> 78 -> 5 -> 2
This list contains 7 number of items
The Number of even numbers is: 5
The number of even nodes removed was: 5
The resulting list is... 23 -> 5
***************After the even numbers are removed I get this back, Are you getting the same problem?***************************************
Now backwards: 5 -> 27303024 -> 27302960 -> 23 -> 27302928 -> 0
This list contains 2 number of items
The sum of all data is: 28
int removeEven(node*& head) {
// double pointer to use the address of pointer head
node ** deleteNode = &head;
//if is the end of the list stop
if(head==NULL)
return 0;
//if is even
if((*deleteNode) -> data %2==0)
{
node * helper = *deleteNode;
*deleteNode = helper -> next;
delete helper;
return 1+ removeEven(helper -> next);
}
//but if is an odd number
else if ((*deleteNode) -> data %2)
{
//traverse to the next node
deleteNode = &(*deleteNode)->next;
//calls itself so that we can start againg to check in the new node.
return removeEven(head -> next);
}
}
I was told that changing the function like this will help but Im getting a lot of errors please help
//Remove even numbers
node* recfunremoveEven(node *head,node *prevnode, int* count) //helper function for remove even nodes
{
if(head==NULL)
return NULL;
if(head->data %2 == 0) //data is even
{
*count+=1;
free(head);
node* next = recfunremoveEven(head->next,head,&count); //recursive call
if(prevnode)
{
prevnode->next = next;
next->previous = prevnode;
}
return next;
}
return recfunremoveEven(head->next,&count);
}
int removeEven(node*& head)
{
int count=0;
recfunremoveEven(head,&count);
return count;
}
When compiling I get the following errors:
g++ -g -std=c++11 -o proftest dlist.h dlist.cpp main.cpp supplied.o
dlist.cpp: In function ‘node* recfunremoveEven(node*, node*, int*)’:
dlist.cpp:30:53: error: cannot convert ‘int**’ to ‘int*’ for argument ‘3’ to ‘node* recfunremoveEven(node*, node*, int*)’ node* next = recfunremoveEven(head->next,head,&count); //recursive call ^
dlist.cpp:38:42: error: cannot convert ‘int**’ to ‘node*’ for argument ‘2’ to ‘node* recfunremoveEven(node*, node*, int*)’ return recfunremoveEven(head->next,&count); ^
dlist.cpp: In function ‘int removeEven(node*&)’: dlist.cpp:45:29: error: cannot convert ‘int*’ to ‘node*’ for argument ‘2’ to ‘node* recfunremoveEven(node*, node*, int*)’ recfunremoveEven(head,&count);
This is the .h file in case someone need to see it
//doubly linked list
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
struct node
{
int data;
node * previous;
node * next;
};
/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied
//Recursively compute and return the number of nodes that contain even number
//in the doubly linked list
int countEven(node *head);
//Recursively remove all the nodes that contain even number in the doubly linked list
//and return the number of nodes removed
int removeEven(node*& head);
removeEvenyou do not touchpreviouspointer fromNodewhich seems to be very suspicious... - PiotrNycz