answersLogoWhite

0

How a heap is created?

User Avatar

Anonymous

13y ago
Updated: 8/20/2019

You should know classes & pointers before heaps.

My idea of a heap is a bit like this:

You have a class or struct(i.g. Heap_t) with a pointer to another of itself, and some data.

Example:

class Heap_t{

public:

Heap_t(){

Pointer=NULL;

}

Heap_t * point;

int data;

};

int main(){

//Variable

Heap_t * heap;//This will hold a pointer to the root of the heap

Heap_t tmp; //Temporary storage for a piece of the heap before it is added

Heap_t * cur; //Temporary storage for the current piece of the heap

//Setup the heap

tmp.data=0;//Make data something meaningful

cur=(Heap_t *) tmp;//Make the root of the heap

heap=cur;//Backup the root of the heap

//Make the heap big

for(int i=1;i<10;i++){

tmp.data=i;//Make the data something meaningfull

(*cur).next=(Heap_t *) tmp; //Add tmp to the heap

}

//Do stuff

//Exit

return(0);

}

You could also use a reference or use it without a pointer or a reference.

This is a bit more like a linked list, but it is an example.

User Avatar

Wiki User

13y ago

What else can I help you with?