Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void kopiec_przywroc(int* A,int i, int heap_size);
  5. void kopiec_buduj(int *A, int heap_size);
  6.  
  7.  
  8.  
  9.  
  10. main()
  11. {
  12.     char x;
  13.     int z;
  14.     int *A;
  15.  
  16.     while (1)
  17.     {
  18.         printf ("\nco chcesz zrobic?");
  19.         printf("\n b - buduj");
  20.         printf("\n w - wyswietl");
  21.         printf("\n u - usun");
  22.         printf("\n w - wstaw");
  23. fflush (stdin);
  24. scanf ("%c", &x);
  25.         switch (x)
  26.             case 'b': printf("podaj liczbe do dodania");
  27.                         fflush (stdin);
  28.                         scanf ("%d",&z);
  29.                     //zarezerwowac pamiec, okreslic dlugosc tablicy A = (int*)malloc(
  30.                         kopiec_buduj(A,z);
  31.                         break;
  32.              
  33.     }
  34.  
  35. }
  36.  
  37. int PARENT(int i)
  38. {
  39.     return (i-1)/2;
  40. }
  41.  
  42. void kopiec_przywroc(int* A,int i, int heap_size)
  43. {
  44.     int temp;
  45.     int max;
  46.     int l = 2*i+1;
  47.     int r = 2*i+2;
  48.     if (l < heap_size && A[l] > A[i])
  49.         max = l;
  50.     else max = i;
  51.  
  52.     if (l < heap_size && A[r] > A[max])
  53.         max = r;
  54.     if (max != i)
  55.     {   temp = A[i];
  56.         A[i] = A[max];
  57.         A[max] = temp;
  58.         kopiec_przywroc (A,max,heap_size);
  59. }
  60. }
  61.  
  62.  
  63.     void kopiec_buduj(int *A, int heap_size)
  64.     {
  65.         int i;
  66.         for(i = heap_size/2; i >=0; i--)
  67.             kopiec_przywroc(A,i,heap_size);
  68.     }
  69.  
  70.  
  71.     int kopiec_usun_max(int *A, int heap_size)
  72.     {
  73.     //  if (heap_size ==0)
  74.         //  printf("kopiec pusty");//do miana
  75.         int max;
  76.          max = A[0];
  77.                 A[0] = A[heap_size-1];
  78.                 heap_size--;
  79.                 kopiec_przywroc(A,0,heap_size);
  80.                 return max;
  81.        
  82.     }
  83.  
  84.     int* kopiec_wstaw(int *A, int *n, int k)
  85.     {
  86.         int i;
  87.         (*n)++;
  88.         i = *n-1;
  89.         A = (int*)realloc(A,*n * sizeof (int));
  90.         while (i>0 && A[PARENT(i)] <k)
  91.         {
  92.             A[i] = A[PARENT(i)];
  93.             i = PARENT(i);
  94.         }
  95.         A[i] = k;
  96.         return A;
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement