0

I'm writing a mergesort on list (don't look at the functions, I still build them). The problem is following: compilator can't read an induction of merge_sort, I don't know why. I'd like to ask you for help. Below is shown the code.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <cmath>
using namespace std;

struct mode
{
    int vol;
    mode *next;
};
void push(mode *&head, int x)
{
    mode *pon = new mode;
    pon->vol = x;
    pon->next = head;
    head = pon;
}
void show(mode *head)
{
    cout << "head-> ";
    mode *p = head;

    while (p != NULL)
    {
        cout << p->vol << " -> ";
        p = p->next;
    }
    cout << "NULL" << endl;
}

void split_list(mode*& p, mode*& p2, mode*& p3)
{
    int middle, counter = 0;
    mode* p_head = p; // p_head, p -> 4,5,6,2,2,1

    while (p->next)
    {
        counter = counter + 1; // 6
        p = p->next;
    }

    counter = counter + 1;
    middle = counter / 2; // 3

    counter = 0;
    p = p_head;
    p3 = p;

    while (counter != middle) // 0!= 3 p,p_head -> 4, p3 -> 5 ||  1!=3 p,p_head ->4->5->p3->6 || 2!=3 p,p_head ->4->5->6 ->p3>2 || ~(3!=3)
    {
        p3 = p3->next;
        counter = counter + 1; // p, p_head -> 4,5,6, p3-> 2,2,1
    }

    p2 = p_head; // p2,p,p_head -> 4,5,6 p3-> 2,2,1

    while (p2->next != p3) // 5!=2 || 6!=2 || ~(2!=2) => p,p_head ->4,5,p2->6=NULL p3->2->2->1
    {
        p2 = p2->next;
    }

    p2->next = NULL; // 4,5,6 p2 -> null , p3 -> 2,2,1
    p2 = p_head;

    //return &*p2,*p3;
    delete p,p_head;

}

void merge_sort (mode*& head)
{
    mode* p = head;
    mode *p2 = NULL, *p3 = NULL;

    if (head && head->next)
    {
        split_list(p, p2, p3);
        cout << p2->vol << endl;
    }
}

int main()
{
    mode* head = NULL;
    mode* head2 = NULL;
    mode* head3 = NULL;

    push(head, 5);
    push(head, 103);
    push(head, 100);
    push(head, 12);
    push(head, 1052);
    push(head, 10);


    show(head);

    merge_sort(head) << endl;
    //show(head);

    system("pause");
}
11684
  • 7,356
  • 12
  • 48
  • 71
Plusce
  • 117
  • 2
  • 14
  • It would help a lot if you included the exact error message the compiler prints. – 11684 May 05 '15 at 13:51
  • Sorry, but what is an "induction"? [Googling "c++ induction of function"](https://www.google.nl/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#q=c%2B%2B%20induction%20of%20function&es_th=1) yields no relevant results as far as I can tell. – 11684 May 05 '15 at 13:53
  • I think he means 'invocation'. – Cameron May 05 '15 at 13:54
  • By the way, did you mean [node](http://www.oxforddictionaries.com/definition/english/node) instead of [mode](http://www.oxforddictionaries.com/definition/english/mode)? – 11684 May 05 '15 at 13:56
  • I mean "induction" as a loading a function into main (this line: "merge_sort(head) << endl;"). Regarding "mode": We usually use this notation to declare any structure (I know, that "node" is "node", by the way, it would be most correctly). – Plusce May 05 '15 at 17:08

1 Answers1

2

In your main function, I found the following line:

merge_sort(head) << endl;

which tries to invoke the method operator<< on the return value of merge_sort which is impossible, because merge_sort is a void.

I think this will behave as expected:

std::cout << merge_sort(head) << std::endl;

As you can see, I used std::. This question on StackOverflow (and its accepted answer) is why: Why is "using namespace std" considered bad practice?

Community
  • 1
  • 1
11684
  • 7,356
  • 12
  • 48
  • 71
  • That's right. I deleted "<<" and function is working. I will check upper thread for the moment. Thank you. – Plusce May 05 '15 at 17:11