Implementation of mergesort
Implementation of mergesort
我在列表上写下一个Mergesort(请勿查看功能,我仍然构建它们)。问题在于:编译器无法阅读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;
试图在merge_sort
的返回值上调用方法operator<<
,这是不可能的,因为merge_sort
是void
。
我认为这将表现为预期:
std::cout << merge_sort(head) << std::endl;
如您所见,我使用了std::
。在Stackoverflow上的这个问题(及其接受的答案)是为什么:为什么使用命名空间std&quot&quot被认为不良练习?
相关文章:
- Usages of std::move
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- Deprecation of _writeBarrier()
- constexpr begin of a std::array
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- C++ "error: invalid use of void expression"
- 我看到"use of undeclared identifier"错误,有人可以告诉我如何解决它吗?
- 赛通"Cannot take address of memoryview slice"
- C++ Version Of Double.longBitsToDouble
- 收到错误"invalid use of non-static data member 'stu::n' "
- C++ Usage of AsyncCallback
- 错误:"Left of getValue must have class/struct/union"
- Centos7 g++ "to_string is not in a member of std"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 传递 std::vector of std::shared_ptr,而不是更新对象
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- Implementation of mergesort