Please help: insert in the middle of a simply linked list
Hello everyone, whats wrong with my code? When i want to capture in specific position, it does, but delete the rest of the list
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int i=1;
pPerson curr, new;
nuevo=new Person();
curr=begin;
while(i!=pos){
curr=curr->next;
i++;
}
new->Capture();
new->next=curr->next;
curr->next=new;
size++;
|
thanks in advance.
1. Use of "new" as identifier should not work.
2. Lines 5-6. What if "pos" is not within the list?
Fix those first.
thanks @keskiverto , here you go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void List::ins_List(int pos){
if(pos<1 || pos>=size){
cout << "Invalido";
}
else{
int i=1;
pPerson curr, nuevo;
nuevo=new Person();
curr=begin;
while(i!=pos){
curr=curr->next;
i++;
}
nuevo->Capturar();
nuevo->next=curr->next;
curr->next=nuevo;
size++;
}
}
}
|
That does not look clearly wrong, but how do you add to an empty list?
I just fixed, just rewrited the whole code in new files and now is working. Thanks =)
Topic archived. No new replies allowed.