I feel very confident that i'm right in this code. Logically, it makes sense to me but for some reason the program refuses to run past a certain point. I'm supposed to do this without using proprietary classes or hashtables. My list node is a basic singly linked list. Assuming i have a dummy list at first, 0, i'm able to add one number to the list but that is all. This is the method that will not work beyond adding the first number.
Assuming my list is 0 -> 2. and i'm trying to add 1.
public void insert(int newElement) {
List marker = head;
List temp = new List(newElement, null);
if (head.next == null) {
head.next = temp;
} else {
while (marker.next != null) {
if (newElement < marker.next.value) {
temp.next = marker.next;
marker.next = temp;
marker = marker.next;
}
}
}
}
This seems to be working and I'm currently testing it with good results. Can anyone find a problem with this code that i'm missing?
This is your code snippet:
Take a pencil and a paper and trace out this piece of code. You will see what's wrong with it. Take a look at this answer and see the image attached. This is how you should really debug problems regarding linked lists. That image is not specific to your code, but it should give you an idea how to approach this kind of problems.