我正在实现二进制堆类.堆实现为动态分配的数组.堆类具有成员容量,大小和指向数组的指针,如下所示:
class Heap
{
private:
Heap* H;
int capacity; //Size of the array.
int size; //Number of elements currently in the array
ElementType* Elements; //Pointer to the array of size (capacity+1)
//I've omitted the rest of the class.
};
Run Code Online (Sandbox Code Playgroud)
我的建筑看起来像这样:
Heap::Heap (int maxElements)
{
H = ( Heap* ) malloc ( sizeof ( Heap ) );
H -> Elements = ( ElementType* ) malloc ( ( maxElements+1 )*sizeof ( ElementType ) );
H -> Elements[0] = DUMMY_VALUE; //Dummy value
H …Run Code Online (Sandbox Code Playgroud)