#include<iostream> #include<cstdlib> #include<ctime> #include<queue> using namespace std; priority_queue <int> pq; int v[100]; int main(void) { int n = 10; srand(time(NULL)); printf("Vector \n"); for(int i = 0; i<n; i++){ v[i] = rand() % 100; printf("%d ", v[i]); } printf("\n\n"); for(int i = 0; i<n; i++){ pq.push(v[i]); } printf("Priority Queue \n"); while(!pq.empty()){ printf("%d ", pq.top()); pq.pop(); } printf("\n\n"); }
#include <stdio.h> // min이 가장 위에 가도록! int heap[32]; //동적할당, 포인터로 바꿔보자 ㅠㅠ //실제 data가 들어가는 index는 0부터가 아니라 1부터임! int push(int num){ int i; for (i = 1; i < 32; i++){ if (heap[i] == -1){ //이거 때문에 일단 양의 정수만 넣을수 있음 (count를 쓰는 방법도 있지만 이걸로 해봄) heap[i] = num; break; } if (i == 31){ printf("the heap is full\n"); return 1; } } while(heap[i] < heap[i / 2]){ int temp_for_change; temp_for_change = heap[i]; heap[i] = heap[i / 2]; heap[i / 2] = temp_for_change; i = i / 2; } } int pop(){ int last_num_index; //숫자가 있는 마지막 칸의 index int j=1, small_num_index; //for 'while' for (int i = 1; i < 32; i++){ if (heap[i] == -1){ if (i == 1){ printf("heap is empty\n"); return 1; } last_num_index = i - 1; break; } if (i == 31){ last_num_index = 31; } } heap[1] = heap[last_num_index]; heap[last_num_index] = -1; while (1){ if ((heap[j] > heap[j * 2] && heap[j * 2] != -1) || (heap[j] > heap[j * 2 + 1] && heap[j*2+1] != -1)){ //children 중에 parent보다 작은놈이 있다! int temp_for_change; if (heap[j * 2] <= heap[j * 2 + 1] || heap[j*2+1] == -1){ //왼쪽을 끌어올린다 small_num_index = j * 2; } else{ //오른쪽을 끌어올린다 small_num_index = j * 2 + 1; } temp_for_change = heap[small_num_index]; heap[small_num_index] = heap[j]; heap[j] = temp_for_change; j = small_num_index; } else{ return 0; } } } void view_top(){ printf("top : %d\n", heap[1]); } /* void view_all(){ //just for debug for (int i = 1; i < 32; i++){ printf("%d ", heap[i]); } printf("\n"); } */ void main(){ int temp; int num; for (int i = 0; i < 32; i++){ heap[i] = -1; } while (1){ printf("push : 1, pop : 2\tinput : "); scanf_s("%d", &temp); switch (temp){ case 1: printf("num : "); scanf_s("%d", &num); push(num); //view_all(); break; case 2: view_top(); pop(); //view_all(); break; default: break; } if (temp != 1 && temp != 2) break; } }
#pragma warning(disable: 4996) #include <stdio.h> #include <stdlib.h> #include <math.h> #define SIZE 50 void fillHeap(int* heap, int number_of_element); void insert(int* heap, int i); void delete(int* heap, int i, int number_of_element); void swap(int* a, int* b); void displayHeap(int* heap, int number_of_element); int main(){ int* heap = (int *)malloc(SIZE*sizeof(int)); int number_of_element = 0; int option = 0; printf("type number of elements : "); scanf("%d", &number_of_element); fillHeap(heap, number_of_element); while (1){ printf("Select(1. push 2. pop 3. show heap 4. exit) : "); scanf("%d", &option); switch (option){ case 1: number_of_element++; printf("Enter new element : "); scanf("%d", heap + number_of_element); insert(heap, number_of_element); break; case 2: *(heap + 1) = *(heap + number_of_element); number_of_element--; delete(heap, 1, number_of_element); break; case 3: displayHeap(heap, number_of_element); break; case 4: printf("Program Ended.\n"); system("pause"); return 0; break; default: printf("Wrong Number. \n"); break; } } return 0; } void fillHeap(int* heap, int number_of_element){ int i = 0, element = 50; for (i = 1; i <= number_of_element; i++){ *(heap + i) = element--; } } void displayHeap(int* heap, int number_of_element){ int i = 0, exponent = 0; for (i = 1; i <= number_of_element; i++){ if (i == pow(2, exponent)){ exponent++; printf("\n"); } printf("%d ", *(heap + i)); } printf("\n"); } void insert(int* heap, int i){ if (*(heap + i / 2) < *(heap + i)){ swap(heap + i / 2, heap + i); } if (i / 2 != 1){ insert(heap, i / 2); } } void delete(int* heap, int i, int number_of_element){ if (*(heap + 2 * i) > *(heap + 2 * i + 1) && 2*i <= number_of_element){ swap(heap + i, heap + 2 * i); delete(heap, 2 * i, number_of_element); } else if(*(heap + 2 * i) < *(heap + 2 * i + 1) && 2*i + i <= number_of_element) { swap(heap + i, heap + 2 * i + 1); delete(heap, 2 * i + 1, number_of_element); } } void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp; }