2016-09-11 3 views
-2

Я пытаюсь сделать процедуру сортировки слияния по сортировке связанного списка, и я не мог понять, почему код не работает, мой компилятор не дает никакого полезного сообщения об ошибке. Разве кто-то не может указать мне на это? благодаря!Ошибка в объединении сортировки связанного списка

#include <stdio.h> 

    #include <stdlib.h> 

    #include <iostream> 

    using namespace std; 

    struct listnode { struct listnode * next; int key; }; 

    //function prototypes 

    void seperate(struct listnode * node, struct listnode ** front, struct listnode ** back); 

    struct listnode * merge(struct listnode * node_1, struct listnode * node_2); 

    //merge sorted seperated linked list 
    void mergeSort(struct listnode ** node) 
    { 
    struct listnode * head = * node; struct listnode * node_1; struct listnode * node_2; 

    if ((head == NULL) || (head->next == NULL)) { return; } 

    seperate(head, &node_1, &node_2); 
    mergeSort(&node_1); mergeSort(&node_2); 
    * node = merge(node_1, node_2); 

} 

//sort sepearted linked list 
struct listnode * merge(struct listnode * node_1, listnode * node_2) 
{ 
    struct listnode * return_result = NULL; 
    if (node_1 == NULL) { return (node_2); } 
    else if (node_2 = NULL) { return (node_1); } 

    if (node_1->key <= node_2->key) 
    { 
     return_result = node_1; return_result->next = merge(node_1->next, node_2); 
    } 
    else { return_result = node_2; return_result->next = merge(node_1, node_2->next); } 

    return return_result; 

} 

//function to seperate linked list 
void seperate(struct listnode * node, struct listnode ** front, struct listnode ** back) 
{ 
    struct listnode * fast; struct listnode * slow; 

    if (node == NULL || node->next == NULL) { *front = node; * back = NULL; } 

    else 
    { 
     slow = node; fast = node->next; 

     while (fast != NULL) 
     { 
      fast = fast->next; 
      if (fast != NULL) { slow = slow->next; fast = fast->next; } 
     } 

     * front = node; * back = slow->next; slow->next = NULL; 

    } 


}// merge sort of linked list completed 



//test functions to push and print the linked list 

void push(struct listnode ** head, int data) 
{ 
    struct listnode * added_node = (struct listnode *)malloc(sizeof(struct listnode)); 
    added_node->key = data; 
    added_node->next = (*head); 
    (*head) = added_node; 
} 

void printList(struct listnode * node) 
{ 
    while (node != NULL) 
    { 
     cout << node->key; 
     node = node->next; 
    } 

} 

int main() 
{ 
    struct listnode * node1 = NULL; 

    push(&node1, 3); push(&node1, 30); push(&node1, 23); push(&node1, 1); push(&node1, 0); push(&node1, 9); 
    mergeSort(&node1); 
    cout << endl; 
    printList(node1); 

    return 0; 
} 
+0

Когда вы говорите «код не работает», что именно вы имеете в виду? –

+0

Я получаю ошибку сегментации, но я не мог понять, где это может быть. – user6820297

+0

* мой компилятор не дает никакого полезного сообщения об ошибке. * - Задача компилятора - убедиться, что ваша программа синтаксически правильная. Работа по поиску * логических * ошибок зависит от вас и от сеансов отладчика/отладки. – PaulMcKenzie

ответ

1
if (node_1 == NULL) { return (node_2); } 
else if (node_2 = NULL) { return (node_1); } 
/// -----------^ this is an assignment here 

Чтобы Йоды познакомить вас я

if (NULL == node_1) { return (node_2); } 
else if (NULL=node_2) { return (node_1); } 
/// ---------^ still an assignment, 
// but try it out & C what the compiler has to say 
+0

nvm lol ...... Я вижу это – user6820297

+0

Спасибо, ребята. Я подумал о своей глупой ошибке. – user6820297

+0

@ user6820297 Заботиться принять ответ тогда? –

Смежные вопросы