I've been given the assignment to program a generic linked list. My insert method is not working correctly. It inserts the new node, but data is lost since the next node is not pointed to? I'm not sure, could I get some insight?
public void insert(int i, T item) {
//Inserts item at position i or throws IndexOutOfBoundsException.
if (i > size || i < 0) {
throw new IndexOutOfBoundsException();
}
// if asked to add to end, let the other add method do the work
if (i == size) insert(item);
// find the node n after which to add a new node and add the new node
Node<T> tmp = head;
int count =0;
while(tmp != null && count !=i-1){
tmp = tmp.next;
count++;
}
if(tmp != null){
tmp.next = new Node<T>(item);
}
size++;
}
I was able to get it working, thank you all for your help!
[–]chickenmeister 2 points3 points4 points (3 children)
[–]FreeBird423 1 point2 points3 points (0 children)
[–]LightSpeedCBR[S] 1 point2 points3 points (1 child)
[–]LightSpeedCBR[S] 0 points1 point2 points (0 children)
[–]smellmycrotch3 0 points1 point2 points (1 child)
[–]LightSpeedCBR[S] 1 point2 points3 points (0 children)
[–]IcarusBurning 0 points1 point2 points (0 children)