1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <stdio.h> int bn; struct Node { int val; Node* prev; Node* next; Node* alloc(int _val, Node* _prev, Node* _next) { val = _val; prev = _prev; next = _next; if (prev) prev->next = this; if (next) next->prev = this; return this; } void del() { if (prev) prev->next = next; if (next) next->prev = prev; prev = next = 0; } }buf[10000], *hash[1000], *hash_tail[1000]; #define MAX_SIZE 100 struct Heap { int val; Node* p; }heap[10000]; int heapSize = 0; int heapPush(int value, Node* _p) { if (heapSize + 1 > MAX_SIZE) { printf("queue is full!"); return 0; } heap[heapSize].val = value; heap[heapSize].p = _p; int current = heapSize; while (current > 0 && heap[current].val < heap[(current - 1) / 2].val) { int temp_val = heap[(current - 1) / 2].val; Node* temp_p = heap[(current - 1) / 2].p; heap[(current - 1) / 2].val = heap[current].val; heap[(current - 1) / 2].p = heap[current].p; heap[current].val = temp_val; heap[current].p = temp_p; current = (current - 1) / 2; } heapSize = heapSize + 1; return 1; } Node* heapPop(int *value) { if (heapSize <= 0) { return 0; } *value = heap[0].val; Node* ret_p = heap[0].p; heapSize = heapSize - 1; heap[0] = heap[heapSize]; int current = 0; while (current * 2 + 1 < heapSize) { int child; if (current * 2 + 2 == heapSize) { child = current * 2 + 1; } else { child = heap[current * 2 + 1].val < heap[current * 2 + 2].val ? current * 2 + 1 : current * 2 + 2; } if (heap[current].val < heap[child].val) { break; } int temp_val = heap[current].val; Node* temp_p = heap[current].p; heap[current].val = heap[child].val; heap[current].p = heap[child].p; heap[child].val = temp_val; heap[child].p = temp_p; current = child; } return ret_p; } int main() { hash[0] = buf[bn++].alloc(0, 0, 0); hash_tail[0] = buf[bn++].alloc(0, 0, 0); hash[0] = hash_tail[0]; hash_tail[0]->prev = hash[0]; int arr[5] = { 3, 2, 5, 4, 1 }; for (int i = 0; i < 5; ++i) { buf[bn].alloc(arr[i], hash_tail[0]->prev, hash_tail[0]); heapPush(arr[i], &buf[bn]); bn++; } for (Node*p = hash[0]->next; p != hash_tail[0]; p = p->next) { printf("val:%d\n", p->val); } int value; Node* del_p = heapPop(&value); printf("pop %d\n", value); del_p->del(); for (Node*p = hash[0]->next; p != hash_tail[0]; p = p->next) { printf("val:%d\n", p->val); } del_p = heapPop(&value); printf("pop %d\n", value); del_p->del(); for (Node*p = hash[0]->next; p != hash_tail[0]; p = p->next) { printf("val:%d\n", p->val); } return 0; } | cs |
'공부 > 알고리즘' 카테고리의 다른 글
MongoDB 시간 groupby 쿼리 (0) | 2024.04.04 |
---|---|
LL 이진탐색트리 (0) | 2020.12.10 |
정렬(sort) 관련 (0) | 2020.12.09 |
부분합 문제 (0) | 2020.12.07 |
기본 dfs, bfs (0) | 2020.12.01 |