Originally Posted by

**claudiu**
You are not visibly allocating memory in that function so it's pretty hard to tell. Post your entire code. Alternatively install Valgrind and run your program using ./valgrind myprog. It will tell you if you are leaking anywhere.

Not that I think it would help understanding the problem but heres the entire code:

Code:

# include <stdio.h>
# include <assert.h>
# include <stdlib.h>
struct node {
int data;
struct node* next;
};
void PrintLinkedList (struct node* head) {
if (head == NULL) {
printf("Null List");
return;
}
while (head -> next != NULL) {
printf("%d ", head -> data);
head = head -> next;
}
printf("%d", head -> data);
return;
}
struct node* BuildOneTwoThree() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc (sizeof(struct node));
second = malloc (sizeof(struct node));
third = malloc (sizeof(struct node));
head -> data = 3;
head -> next = second;
second -> data = 2;
second -> next = third;
third -> data = 1;
third -> next = NULL;
return (head);

}
void Push (struct node** headref, int data) {
struct node* first = malloc (sizeof(struct node));
first -> data = data;
first -> next = *headref;
*headref = first;

}
void DeleteList (struct node** headRef) {
struct node* current = *headRef;
struct node* ahead;
while (current != NULL) {
ahead = current -> next;
free (current);
current = ahead;
}
*headRef = NULL;
}
void SortedInsert (struct node** headRef, struct node*
newNode) {
struct node* current = *headRef;
if(*headRef == NULL || newNode -> data <= (*headRef) ->
data) {
newNode -> next = *headRef;
*headRef = newNode;
return;
}
while (current -> next != NULL && newNode -> data >
(current -> next) -> data)
current = current -> next;
newNode -> next = current -> next; //WATCH THE ORDER HERE!
current -> next = newNode;
//WATCH THE ORDER HERE!
return;

}
void InsertSort (struct node** headRef) {
struct node* newlist = NULL;
struct node* current = *headRef;
while (current != NULL) {
SortedInsert (&newlist, current);
current = current -> next;
}
SortedInsert (newlist, current);
DeleteList (*headRef);
*headRef = newlist;
return;

}
int main() {
struct node* head = BuildOneTwoThree();
SortedInsert(&head,anode);
PrintLinkedList (head);
getchar();

}

I have not run the entire consolidated code yet so there may be errors.