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.
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.Code:int sortList(ListNodePtr *sPtr, char* string, int value){ ListNodePtr head; ListNodePtr previousPtr; ListNodePtr newPtr; head = *sPtr; previousPtr = NULL; newPtr = malloc(sizeof(ListNode)); /*create node*/ if(newPtr != NULL) { /*is space available*/ newPtr->symbol = strdup(string); /*set newPtr values to be ready to insert it*/ newPtr->value = value; newPtr->nextPtr = NULL; 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; } }



LinkBack URL
About LinkBacks


