struct Heap {
int capacity;
int heapSize;
int *tree; // the heap binary tree
int *pos; // pos[i] is the position of values[i] in items
float *p; // priority value of each heap element
};
void initHeap(struct Heap *heap, int capacity) {
heap->capacity = capacity;
heap->heapSize = 0;
heap->tree = malloc(sizeof(int)*(capacity+1));
heap->pos = malloc(sizeof(int)*(capacity+1));
heap->p = malloc(sizeof(float)*(capacity+1));
}
void betterInit(struct Heap *heap, int capacity) {
with (heap) { // doesn't exist
capacity = capacity;
heapSize = 0;
tree = malloc(sizeof(int)*(capacity+1));
pos = malloc(sizeof(int)*(capacity+1));
p = malloc(sizeof(float)*(capacity+1));
data = malloc(sizeof(struct Data)*(capacity+1));
}
}
所以第一个initHeap
看起来很长,因为我不得不多次写heap->
。我希望使它看起来更短。
一种解决方法是写:
int *tree = heap->tree;
int *pos = heap->pos;
float *p = heap->p;
然后使用tree, pos, p
。还有更多方法吗?谢谢。
如何使用点“。” ?喜欢