Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- void kopiec_przywroc(int* A,int i, int heap_size);
- void kopiec_buduj(int *A, int heap_size);
- main()
- {
- char x;
- int z;
- int *A;
- while (1)
- {
- printf ("\nco chcesz zrobic?");
- printf("\n b - buduj");
- printf("\n w - wyswietl");
- printf("\n u - usun");
- printf("\n w - wstaw");
- fflush (stdin);
- scanf ("%c", &x);
- switch (x)
- case 'b': printf("podaj liczbe do dodania");
- fflush (stdin);
- scanf ("%d",&z);
- //zarezerwowac pamiec, okreslic dlugosc tablicy A = (int*)malloc(
- kopiec_buduj(A,z);
- break;
- }
- }
- int PARENT(int i)
- {
- return (i-1)/2;
- }
- void kopiec_przywroc(int* A,int i, int heap_size)
- {
- int temp;
- int max;
- int l = 2*i+1;
- int r = 2*i+2;
- if (l < heap_size && A[l] > A[i])
- max = l;
- else max = i;
- if (l < heap_size && A[r] > A[max])
- max = r;
- if (max != i)
- { temp = A[i];
- A[i] = A[max];
- A[max] = temp;
- kopiec_przywroc (A,max,heap_size);
- }
- }
- void kopiec_buduj(int *A, int heap_size)
- {
- int i;
- for(i = heap_size/2; i >=0; i--)
- kopiec_przywroc(A,i,heap_size);
- }
- int kopiec_usun_max(int *A, int heap_size)
- {
- // if (heap_size ==0)
- // printf("kopiec pusty");//do miana
- int max;
- max = A[0];
- A[0] = A[heap_size-1];
- heap_size--;
- kopiec_przywroc(A,0,heap_size);
- return max;
- }
- int* kopiec_wstaw(int *A, int *n, int k)
- {
- int i;
- (*n)++;
- i = *n-1;
- A = (int*)realloc(A,*n * sizeof (int));
- while (i>0 && A[PARENT(i)] <k)
- {
- A[i] = A[PARENT(i)];
- i = PARENT(i);
- }
- A[i] = k;
- return A;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement