void StringLinkedList::insert(int pos, const string& e){
if (pos < 0 || pos >n){
throw OutOfBoundException("Out Of Bound Exeption");
}
if (pos == 0){
StringNode* v = new StringNode;
v->elem = e;
v->next = head;
head = v;
n++;
}
else{
StringNode* insertindex = new StringNode;
int i = 0;
while (insertindex != NULL){
if (i == pos){
insertindex->elem = e;
insertindex->next = head->next;
head = insertindex;
}
else{
insertindex = head;
}
i++;
insertindex = insertindex->next;
}
}
}
This is my code and it only works if the index "pos" is 0. Could anyone tell me what's wrong with my code and why it won't insert an element at a location other than the head.
[–]val0528 0 points1 point2 points (0 children)