rendered paste body#include<stdio.h>
#include<stdlib.h> //ptr malloc
#include<string.h> //ptr memcpy
struct heap{
int nheap;
int max;
void **a;
};
typedef struct heap* PQ;
PQ PQ_New(int max){
PQ h;
h=(PQ)malloc(sizeof(struct heap));
h->nheap=0;
h->max=max;
h->a=(void**)malloc(max*sizeof(void*));
return h;
}
void interschimba(void* x, void* y, int size){
void* aux;
aux=malloc(size);
memcpy(aux,x,size);
memcpy(x,y,size);
memcpy(y,aux,size);
free(aux);
}
void UrcaValoare(PQ h, int poz){
if (poz/2>1 && h->a[poz]>h->a[poz/2])
{
interschimba(h->a[poz], h->a[poz/2], sizeof(int));
poz=poz/2;
}
}
void Adauga(PQ h, void* x){
//%assert(!)
h->nheap++;
h->a[h->nheap]=x;
UrcaValoare(h,h->nheap);
}
int main(){
int v[30], i ;
PQ h;
v[1]=2; v[2]=9; v[3]=5;
v[4]=3; v[5]=6; v[6]=10;
h=PQ_New(15);
for(i=1;i<=6;i++)
Adauga(h,&v[i]);
for (i=1;i<=h->nheap;i++)
printf("%d ",*(int*)h->a[i]);
return 0;
}