I'm trying to create a function for a Single Linked list that, starting from the i (argument) member, deletes every 2nd node. I'm doing this for benchmark purposes, I already have it working nicely in Java, but I haven't done much programming in C++ for a few years, so I'm a bit rusty.
void List::remove(long i) {
bool changelastonfirstrun=false;
if(i==size){
changelastonfirstrun=true;
}
if(size==0||i>size){
cout <<"Index out of bounds or empty list";
}
else{
Node* aux=first;
Node* aux2=new Node();
int j=1;
while(size>1&&i>1){
for(j=1; j<(i-1); j++){
aux=aux->Next();
}
aux2=NULL;
aux2=aux->Next();
aux->SetNext(aux2->Next());
size--;
i=i-2;
if(changelastonfirstrun){
last=aux;
changelastonfirstrun=false;
}
if(i==1){ //updates reference if removing the first
if(size>1){
first=first->Next();
size--;
}
if(size==1){ //Removing a unique member
clear();
}
}
}
delete aux, aux2;
}
}
I'm getting the problem at
while(size>1&&i>1){
for(j=1; j<(i-1); j++){
aux=aux->Next();
}
As soon as I decrease i for the first time. If you need any more information, just tell me. Thank you.