Share
Explore BrainMass

Write a C++ program to take n elements and insert them into a heap

I need help with this assignment for my discussion forum. It says to write a program to take n elements and insert them into a heap one by one. Include a function to print out the elements in the heap.

Solution Preview

#include <stdlib.h>

#include<iostream>
using namespace std;
#define MinPQSize (10)
#define MinData (-32767)
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%sn", Str ), exit( 1 )

struct HeapStruct
{
int Capacity;
int Size;
int *Elements;
};
// typedef int int;

struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;

PriorityQueue Initialize( int MaxElements );
void Destroy( PriorityQueue H );
void MakeEmpty( PriorityQueue H );
void Insert( int X, PriorityQueue H );
int DeleteMin( PriorityQueue H );
int FindMin( PriorityQueue H );
int IsEmpty( PriorityQueue H );
int IsFull( PriorityQueue H );
int main(){
int i;
PriorityQueue p;
p = Initialize(10);
...

Solution Summary

The solution gives a complete C++ code to write a C++ program to take n elements and insert them into a heap one by one.

$2.19