[英]Implementation of mergesort
我在列表上寫了一個mergesort(不看功能,我仍然在構建它們)。 問題出在下面:編譯器無法讀取歸納歸並的信息,我不知道為什么。 我想請你幫忙。 下面顯示了代碼。
#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");
}
在您的main
功能中,我發現了以下幾行:
merge_sort(head) << endl;
它試圖在merge_sort
的返回值上調用方法operator<<
,這是不可能的,因為merge_sort
是一個void
。
我認為這將符合預期:
std::cout << merge_sort(head) << std::endl;
如您所見,我使用了std::
。 關於StackOverflow(及其公認的答案)的這個問題是為什么: 為什么“使用命名空間標准”被認為是不好的做法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.