Remove all odd integers from a doubly linked list. My remove function is not wor
ID: 3721591 • Letter: R
Question
Remove all odd integers from a doubly linked list.
My remove function is not working for the case of when 2 odd integers are next to each other.
How can I fix this?
117 int remove_from(node*&head)
118 {
119 //if head is to be deleted
120 if(!head->previous)
121 {
122 node * temp = head;
123 head = temp -> next;
124 // head -> previous = NULL;
125 delete temp;
126 temp = NULL;
127 return 0;
128 }
129
130 //end of the list
131 if(!head->next )
132 {
133 node * temp = head;
134 head = temp -> previous;
135 head -> next = NULL;
136 delete temp;
137 temp = NULL;
138 return 0;
139 }
140 else
141 {
142 node * temp = head;
143 temp -> previous -> next = temp -> next;
144 delete temp;
145 temp = NULL;
146 return 0;
147 }
148
149 return 0;
150 }
int remove_odd(node*&head)
208 {
209 if(!head)
210 return 0;
211
212 if(head -> data % 2 ==1)
213 {
214 remove_from(head);
215 }
216
217 return remove_odd(head->next);
218 }
Explanation / Answer
Your code is failing in case of consecutive odd numbers becuase when you remove a node from the list, you just change its next pointer. I am providing an alternative implementation of remove_odd()