I am creating a linked list function for homework that adds at any index except last, but I don't understand how to make a conditiontargetList.addToIndexAt(81,0);
without sentinel nodes
EDIT Okay, I fixed all of the problems, except for one. This time, the code runs the code states that the result is 81,0,0,0,0
, which means that after
returns back to 0 every cycle of the code. How do i make the after=after.tail retain it's number?
public void addToIndexAt(int n, int index){
IntList addition = new IntList(n);
if(index==0){ //THIS IS MY PROBLEM
IntList beginning=this;
IntList after=this;
IntList current=this;
IntList temp=this;
while(after.tail!=null){
after=after.tail;
temp=after;
after.head=current.head;
}
beginning.head=n;
}
else{
IntList after = this;
IntList before = this;
int nafter = index;
int nbefore = index;
while(nafter>0){
after = after.tail;
nafter--;
}
addition.tail = after;
while(nbefore>1){
before = before.tail;
nbefore--;
}
before.tail= addition;
}
}