Over the past couple days, I have been trying all kinds of different methods or rearranging a linked list by value. Everytime I though I had it, I would find some order in which the input would cause it not to rearrange properly. My current method seems to work perfect except for 1 small problem. When I increase the value of one of the nodes to the point where it is the biggest number in the list, it just disappears.

my call to the function is...
sortList(&head, tempString, curVal);
where head is the start of the list.

while(head != NULL) {
/*if its greater than the first node,
insert it at the start. This must be where the issue is,
but I used this same code in an earlier section of my program
and it correctly inserted the node at the start.*/
if(value > head->value && previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
break;
}
/*put the node between previous and head*/
else if(value > head->value && previousPtr != NULL) {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = head;
break;
}
else if(head->nextPtr == NULL) {
head->nextPtr = newPtr;
break;
}

previousPtr = head; /*move to next node*/
head = head->nextPtr;
}
}

I am not sure if anyone can understand this since I didnt put very much code up, but since its an assignment I dont want to put it all up. I have mainly been messing around with passing head or &head. Also with using sPtr and *sPtr. I feel like the problem could have something to do with that.

10-20-2011

anduril462

Next time, more complete code might be helpful, so I can just copy-paste, compile and debug. I worked up a test program around this easily enough though. You should also learn to use a debugger, so you can step through and see exactly what's happening at each step of the program. That's how I solved this. It took me about 30 seconds to find the problem.

You don't cover the case of inserting int an empty list. That is, if head is NULL, you skip your whole insert loop. You need something like: