代码拉取完成,页面将自动刷新
同步操作将从 方瑾/async 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "heap.h"
Heap createHeap(HeapEqual equal, HeapType type) {
Heap heap = newHeap();
if (heap == NULL) {
return NULL;
}
heap->equal = equal;
heap->list = (HeapItem)malloc(sizeof(struct heapItem) * 10);
heap->size = 0;
heap->maxSize = 10;
heap->type = type;
heap->insert = insert;
heap->remove = heapRemoveRoot;
return heap;
}
void insert(Heap heap, let item) {
if (heap->size >= heap->maxSize) {
heap->maxSize *= 2;
HeapItem list = realloc(heap->list, sizeof(struct heapItem) * heap->maxSize);
if (list == NULL) {
return;
}
heap->list = list;
}
heap->list[heap->size].data = item;
int index = heap->size;
heap->size++;
while (index != 0) {
int parent = (index - 1) / 2;
int result = heap->equal(heap->list[index].data, heap->list[parent].data);
if ((heap->type == MaxHeap && result > 0) || (heap->type == MinHeap && result < 0)) {
struct heapItem temp = heap->list[parent];
heap->list[parent] = heap->list[index];
heap->list[index] = temp;
}
index = parent;
}
}
let heapRemoveRoot(Heap heap) {
if (heap->size != 0) {
heap->size--;
if (heap->size == 0) return heap->list[0].data;
struct heapItem head = heap->list[0];
struct heapItem tail = heap->list[heap->size];
int index = 0;
int target = 2 * index + 1;
int equalResut = heap->type == MinHeap ? 1 : -1;
while (target <= heap->size - 1) {
if (target < heap->size - 1 && heap->equal(heap->list[target].data, heap->list[target + 1].data) == equalResut) {
target++;
}
if (heap->equal(tail.data, heap->list[target].data) == -equalResut) break;
heap->list[index] = heap->list[target];
index = target;
target = 2 * index + 1;
}
heap->list[index] = tail;
return head.data;
}
if (heap->size < heap->maxSize) {
heap->maxSize /= 2;
HeapItem list = realloc(heap->list, sizeof(struct heapItem) * heap->maxSize);
heap->list = list;
}
return NULL;
}
void freeHeap(Heap * heap) {
free((*heap)->list);
free(*heap);
*heap = NULL;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。