我有一個函數,它應該採取兩個鏈表並將它們放在一起。C兩個鏈表添加並排序
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);
}
我想對它進行排序,但我不斷收到分段錯誤,當我更換這個while循環:
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;
}
}
的推送()函數只是增加了一些數據到結構節點:
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;
};
我剛剛意識到這將不會完全排序列表 –