Explore BrainMass
Share

Explore BrainMass

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

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    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.

    © BrainMass Inc. brainmass.com October 10, 2019, 7:22 am ad1c9bdddf
    https://brainmass.com/computer-science/cpp/write-a-c-program-to-take-n-elements-and-insert-them-into-a-heap-578683

    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