Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int * kopiec_przywroc(int*, int, int*);
- int * kopiec_buduj(int*, int*, int);
- //kopiec_usun_max();
- int * kopiec_wstaw(int*, int, int*);
- //kopiec_sort();
- int parent(int);
- int main(){
- int *A = calloc(1,sizeof(int)), length_a = 0, heap_size = 0;
- int quit = 1, i;
- char znak;
- A[0] = 0; // heap_size
- system("cls");
- while(quit){
- printf("\nWybierz akcje: ");
- printf("\n[D] - Dodaj element do kopca");
- printf("\n[U] - Usun maksymalny element");
- printf("\n[S] - Posortuj");
- printf("\n[W] - Wyswietl tablice");
- printf("\n[Q] - Wyjdz");
- fflush(stdin);
- scanf("%c", &znak);
- system("cls");
- switch(znak){
- case 'd':
- printf("Podaj wartosc do dodania: ");
- scanf("%d", &i);
- A = kopiec_wstaw(A, i, &heap_size);
- break;
- case 'u':
- break;
- case 's':
- break;
- case 'w':
- for(i = 1; i <= heap_size; i++)
- printf("%d ", A[i]);
- break;
- case 'q':
- quit = 0;
- break;
- }
- }
- free(A);
- return 0;
- }
- int *kopiec_wstaw(int *table, int key, int * heap_size){
- int i = ++(*heap_size);
- int pi = parent(i);
- table = realloc(table,*(heap_size)*sizeof(int));
- while (i > 1 && table[pi] < key){
- table[i-1] = table[pi];
- i = pi;
- }
- table[i] = key;
- return table;
- }
- int parent(int i){
- return (i-1)/2;
- }
- int * kopiec_przywroc(int *table, int i, int heap_size){
- int l = 2 * i, r = 2 * i + 1;
- int max, temp;
- if (l <= heap_size && table[l] > table[i])
- max = l;
- else
- max = i;
- if (r <= heap_size && table[r] > table[max])
- max = r;
- if (max != i){
- temp = table[max];
- table[max] = table[i];
- table[i] = temp;
- kopiec_przywroc(table,max,heap_size);
- }
- return table;
- }
- int * kopiec_buduj(int * table, int *heap_size, int length_a){
- int i;
- *(heap_size) = length_a;
- for (i = *(heap_size)/2; i > 1; i--){
- table = kopiec_przywroc(table, i, heap_size);
- }
- return table;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement