Advertisement
Sawy3R11

AiSD_lab_Kopce_prawienicniemaxD

May 31st, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int parent(int i);
  5. int left(int i);
  6. int right (int i);
  7.  
  8. int *kopiec_wstaw(int *A, int key, int *heap_size);
  9.  
  10.  
  11. int main()
  12. {
  13.     int *tablica = NULL;
  14.     int heap_size = -1;
  15.  
  16.     tablica = kopiec_wstaw(tablica, 10, &heap_size);
  17.    
  18.     tablica = kopiec_wstaw(tablica, 20, &heap_size);
  19.     printf("\n1. wstawiony element: %i", tablica[0]);
  20.     printf("\n2. wstawiony element: %i", tablica[1]);  
  21.    
  22.     getchar();
  23.     return 0;
  24. }
  25.  
  26. int parent(int i)
  27. {
  28.     return (i-1)/2;
  29. }
  30. int left(int i)
  31. {
  32.     return 2*i;
  33. }
  34. int right (int i)
  35. {
  36.     return 2*i+1;
  37. }  
  38.  
  39.  
  40.  
  41. int *kopiec_wstaw(int *A, int key, int *heap_size)
  42. {
  43.     int i;
  44.     (*heap_size)++;
  45.     A = (int *) realloc(A, (*heap_size+1)*sizeof(int));
  46.     i= *heap_size;
  47.     while (i>0 && A[parent(i)]<key)
  48.     {
  49.         printf("petla");
  50.         A[i] = A[parent(i)];
  51.         i = parent(i);
  52.     }
  53.    
  54.     printf("\nkey: %i", key);
  55.     A[i] = key;
  56.  
  57.     return A;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement