gpt4 book ai didi

c++ - 归并排序的实现

转载 作者:行者123 更新时间:2023-11-28 02:27:29 25 4
gpt4 key购买 nike

我正在对列表编写合并排序(不要看函数,我仍然构建它们)。问题如下:编译器无法读取 merge_sort 的归纳,我不知道为什么。我想请你帮忙。下面显示了代码。

#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;

它试图调用方法 operator<<关于 merge_sort 的返回值这是不可能的,因为merge_sortvoid .

我认为这会按预期运行:

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

如你所见,我使用了 std:: . StackOverflow 上的这个问题(及其接受的答案)是为什么:Why is "using namespace std" considered bad practice?

关于c++ - 归并排序的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30036757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com