如有改进的地方,欢迎讨论
bool heap_insert_value(PMinHeap pq, int value){ if(pq == NULL || pq -> heap_size == pq -> capacity){ return false; } PMinHeapNode temp = (PMinHeapNode)malloc(sizeof(MinHeapNode)); temp -> value = value; int i = pq -> heap_size; pq -> heap_array[i] = *temp ; while(i > 0){ if(pq -> heap_array[i].value < pq -> heap_array[(i - 1) / 2].value){ *temp = pq -> heap_array[i]; pq -> heap_array[i] = pq -> heap_array[(i - 1) / 2]; pq -> heap_array[(i - 1) / 2] = *temp; i = (i - 1) / 2; } else{ break; } } pq -> heap_size++; return true; }