主要是从同学这打听到的,于是自己也写了一下。第一道就是叫手写一个堆排序,第二道是写幂,第三道链表反转。
堆排序
堆排序其实就是叫建堆嘛,最大堆和最小堆,之前也写过,具体见 堆和堆排序。这次写的时候写成了模板,想着要好点。
#include <iostream>
#include <algorithm>
using namespace std;
// 两个比较仿函数,STL有自带的,也可用自带的 greater 和 less
template<typename type>
struct mgreater{
bool operator()(const type& a, const type& b){return a>b;}
};
template<typename type>
struct msmaller{
bool operator()(const type& a, const type& b){return a<b;}
};
template<typename type, typename COMP>
class heap{
private:
type *heaptop; // 用数组存储,来构建堆
int len; // 存储堆大小
COMP comp;
public:
heap(int length){
heaptop = new type[length]; // 预分配堆大小
len = 0;
}
~heap(){ delete [] heaptop;} //