found some questions from the Stanford CS library, and this is the question as follows
Write a SortedInsert() function which given a list that is sorted in increasing order, and a single node, inserts the node into the correct sorted position in the list. While Push() allocates a new node to add to the list, SortedInsert() takes an existing node, and just rearranges pointers to insert it into the list. There are many possible solutions to this problem.
I found a interesting solution that i have a hard time understanding
void SortedInsert(struct node** headRef, struct node* newNode) {
struct node **pp, *curr;
pp = headRef;
while ((curr = *pp) != NULL && curr->data <= newNode->data)
pp = &curr->next;
*pp = newNode;
newNode->next = curr;
}
can someone explain to me how this works? I understand that curr is set to *pp(headRef), curr is set to *pp in the while loop,then checks if the current node is less than the Node to be inserted to, then it sets pp to the next node of the current one. What trips me up is that when the condition fails and jumps to
*pp = newNode;
newNode -> next = curr;
since curr is reset in the while loop to *pp, how does the newNode get connected by the one behind it?? Or am I totally misreading this solution...
