void delete_priority(element del, int *n) { if (!inheap(del, *n)) { printf("The element you want to delete is not in the heap.\n"); return; } change_priority(del, heap[1], n); delete_max_heap(n); }
// 주함수 void main() { element e1={10}, e2={5}, e3={30}; element e4, e5, e6; HeapType heap; // 히프 생성 init(&heap); // 초기화 // 삽입 insert_max_heap(&heap, e1); insert_max_heap(&heap, e2); insert_max_heap(&heap, e3); // print_heap(&heap); // 삭제 e4 = delete_max_heap(&heap); printf("< %d > ", e4.key); e5 = delete_max_heap(&heap); printf("< %d > ", e5.key); e6 = delete_max_heap(&heap); printf("< %d > \n", e6.key); heap_sort(test,5); }
// 우선 순위 큐인 히프를 이용한 정렬 void heap_sort(element a[], int n) { int i; HeapType h; init(&h); for(i=0;i<n;i++){ insert_max_heap(&h, a[i]); } for(i=(n-1);i>=0;i--){ a[i] = delete_max_heap(&h); } }
int main() { Heap *h; int num; char op; h = (Heap *) malloc(sizeof(Heap)); init_heap(h); while (1) { printf("please input option:\n"); scanf("%c", &op); FLUSH_STDIN; if (op == 'a') { printf("please input will add num:\n"); scanf("%d", &num); FLUSH_STDIN; add_heap(h, num); PRT_HEAP(h); } else if (op == 'd') { delete_max_heap(h); PRT_HEAP(h); } else if (op == 'f') { printf("the max is %d\n", find_max_heap(h)); } else { printf("exit program.\n"); PRT_HEAP(h); break; } } }