here is the code

now what i cant fathom out is why when i set mylist.head = young_current->next (line 91) it sets the head pointer to the address pointed to by young_current->next as it should however the next line sets the pointer young_current->next to NULL (line 92) why the dickens does it change what sorted_list.head points at????????Code:#include <stdio.h> #include <stdlib.h> typedef struct Node { int number; struct Node *next; }Node; typedef struct Linkedlist { struct Node *head; struct Node *tail; }Linkedlist; int main() { int i, numbers[7] = {1,4,2,5,7,9,3}; Node *tmp_node = NULL, *current; Linkedlist mylist, sortedlist; mylist.head = NULL; mylist.tail = NULL; sortedlist.head = NULL; sortedlist.tail = NULL; for (i = 0; i < 7; i++) { tmp_node = malloc(sizeof(tmp_node)); if (!tmp_node) { printf("error aquiring memory\n"); break; } tmp_node->number = numbers[i]; if (!mylist.head) // list empty { mylist.head = tmp_node; mylist.tail = mylist.head; } else if (!mylist.head->next) //second node { mylist.head->next = tmp_node; mylist.tail = tmp_node; mylist.tail->next = NULL; } else // another node to append { tmp_node->next = NULL; mylist.tail->next = tmp_node; mylist.tail = tmp_node; } } //print list current = mylist.head; while (current) { printf("number = %d\n", current->number); current = current->next; } //sort list int youngest; Node *current_loop = mylist.head, *previous = NULL, *young_current, *young_previous; while (current_loop) { current = current_loop; previous = NULL; youngest = current_loop->number; young_current = current; young_previous = previous; while (current) { if (youngest > current->number) { youngest = current->number; young_current = current; young_previous = previous; } previous = current; current = current->next; } //remove node from list if (young_current == mylist.head) //young current is the head { mylist.head = young_current->next; young_current->next = NULL; } else if (!young_current->next) //young current is the tail { young_previous->next = NULL; } else //middle node { young_previous->next = young_current->next; young_current->next = NULL; } //add node to sorted list if (!sortedlist.head) // list empty { sortedlist.head = young_current; sortedlist.tail = sortedlist.head; } else if (!sortedlist.head->next) //second node { sortedlist.head->next = young_current; sortedlist.tail = young_current; sortedlist.tail->next = NULL; } else // another node to append { tmp_node->next = NULL; sortedlist.tail->next = young_current; sortedlist.tail = young_current; } current_loop = current_loop->next; } //print list current = sortedlist.head; while (current) { printf("number = %d\n", current->number); current = current->next; } current = sortedlist.head; while (current) { tmp_node = current->next; free(current); printf("node freed\n"); current = tmp_node; } return 0; }

many thanks

coop