I have a function which is supposed to take two linked lists and put them together.
void Append(struct node** aRef, struct node** bRef){
struct node* first = *aRef;
struct node* second = *bRef;
struct node* temp = NULL;
while(first != NULL || second != NULL){
Push(&temp, first->data);
Push(&temp, second->data);
first = first->next;
second = second->next;
}
*aRef = temp;
DeleteList(&second);
}
I want to sort it but I keep getting a segmentation fault when I replace the while loop with this :
while(first != NULL || second != NULL){
if(first->data < second->data){
Push(&temp, first->data);
first = first->next;
}
else{
Push(&temp, second->data);
second = second->next;
}
}
The Push() function just adds some data to a struct node:
void Push(struct node** headRef, int data){
struct node* new = malloc(sizeof(struct node));
new->data = data;
new->next = *headRef;
*headRef = new;
}
struct node{
int data;
struct node* next;
};
This solves your problem. Because if you dot not test both, you can not perform the first comparation.