1 2 3

Inserting a node into a singly linked list

Nodeptr insertsort (Nodeptr*list, int item)


Nodeptrnp;
if ( (np = mknode(item)) != NULL) {
Nodeptrcurr = *list;
Nodeptrprev = NULL;

/* locate the position of this node in the list */
while (curr != NULL && item < curr->data) {
prev = curr;
curr = curr->next;
}
/* let this node point to the next node in the list */
np->next = curr;


/* let the previous node in the list point to this node */
if (prev != NULL)
prev->next = np;
else
/* this node is the first in the list */
*list = np;

} else exit (1);
return np;

[CONVERTED BY MYRMIDON]