Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define rodzaj(type,scan)                                               \
  6.   (                                                                     \
  7.   strcmp(#type, "int")   == 0 ? "%d" :                                  \
  8.   strcmp(#type, "char")  == 0 ? "%c" :                                  \
  9.   strcmp(#type, "double") == 0  && scan == 1 ? "%lf" :                  \
  10.   strcmp(#type, "double") == 0 || strcmp(#type, "float") == 0 ? "%.2f": \
  11.   "blad"                                                                \
  12.   )
  13. #define porownaj(a,b)      \
  14.   (                        \
  15.   a > b ? 1  :             \
  16.   a < b ? -1 :             \
  17.   0                        \
  18.   )
  19. #define print(type,zm) printf(rodzaj(type,0), zm)
  20. #define scan(type,zm) scanf(rodzaj(type,1), &zm)
  21. #define TYP int
  22.  
  23. typedef struct element
  24. {
  25.         TYP wartosc;
  26.         struct element *lewe, *prawe;
  27. } wezel;
  28.  
  29. wezel *wstaw(wezel *drzewo, TYP wart)
  30. {
  31.  
  32.     if(drzewo == NULL)
  33.     {
  34.         wezel *nowy;
  35.         nowy = malloc(sizeof(wezel));
  36.         nowy->wartosc = wart;
  37.         nowy->lewe = NULL;
  38.         nowy->prawe = NULL;
  39.         return nowy;
  40.     }
  41.     else
  42.     {
  43.         if(porownaj(wart,drzewo->prawe)) drzewo->prawe = wstaw(drzewo->prawe,wart);
  44.         else if(porownaj(wart,drzewo->lewe)==-1) drzewo->lewe = wstaw(drzewo->lewe,wart);
  45.     }
  46.     return drzewo;
  47. }
  48. void wypisz(wezel *drzewo)
  49. {
  50.     if(drzewo->lewe != NULL) wypisz(drzewo->lewe);
  51.     print(TYP, drzewo->wartosc);
  52.     fputc(' ',stdout);
  53.     if(drzewo->prawe != NULL) wypisz(drzewo->prawe);
  54.  
  55. }
  56. void usun(wezel *drzewo)
  57. {
  58.     if(drzewo != NULL)
  59.     {
  60.         usun(drzewo->lewe);
  61.         usun(drzewo->prawe);
  62.         free(drzewo);
  63.     }
  64. }
  65. int main()
  66. {
  67.         wezel *drzewo;
  68.         drzewo=malloc(sizeof(wezel));
  69.         drzewo->lewe=drzewo->prawe=NULL;
  70.         wstaw(drzewo,12);
  71.         wstaw(drzewo,2);
  72.         wstaw(drzewo,10);
  73.         wypisz(drzewo);
  74.         usun(drzewo);
  75.         return 0;
  76. }
Add Comment
Please, Sign In to add comment