Advertisement
Guest User

Untitled

a guest
May 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #define VELIME 14
  5. #define M 7
  6. #define ever (;;)
  7.  
  8. struct zapis {
  9.     char ime[VELIME + 1];
  10.     struct zapis *sljed;
  11. };
  12. typedef struct zapis zapis;
  13.  
  14. int Adresa(char ime[]){
  15.     int suma = 0, i;
  16.     for (i=0; i < strlen(ime); i++){
  17.         suma += (int) ime[i];
  18.     }
  19.     return suma % M;
  20. }
  21.  
  22. void Upis (zapis *hash[], struct zapis *element){
  23.     int i = Adresa (element -> ime);
  24.     element -> sljed = hash [i];
  25.     hash [i] = element;
  26. }
  27.  
  28. void Isprazni(zapis *hash[], int n){
  29.     int i;
  30.     for (i=0; i<n; i++){
  31.         hash[i] = NULL;
  32.     }
  33. }
  34.  
  35. int Trazi(zapis *hash[], zapis *element){
  36.     int i = Adresa (element->ime);
  37.     zapis *pom = hash[i];
  38.     while (pom != NULL){
  39.         if (strcmp (pom->ime, element -> ime) == 0)
  40.             return 1;
  41.         pom = pom -> sljed;
  42.     }
  43.     return 0;
  44. }
  45.  
  46. void Ispis(zapis* hash[], int n){
  47.     int i;
  48.     zapis *pom;
  49.     printf ("\nIspis sadrzaja hash-a\n");
  50.     for (i=0; i<n; i++){
  51.         printf ("%d > ", i);
  52.         pom = hash [i];
  53.         while (pom != NULL){
  54.             printf (" %s", pom -> ime);
  55.             pom = pom -> sljed;
  56.         }
  57.         printf ("\n");
  58.     }
  59. }
  60.  
  61. void Obrisi_Hash (zapis **hash, int n){
  62.     int i;
  63.     zapis *pom;
  64.     for (i=0; i<n; i++){
  65.         pom = hash[i];
  66.         while (pom != NULL){
  67.             hash[i] = pom -> sljed;
  68.             free (pom);
  69.             pom = hash[i];
  70.         }
  71.     }
  72.     free (hash);
  73. }
  74.  
  75. int main (void){
  76.  
  77.     zapis **hash;
  78.     zapis *element = NULL;
  79.     int find;
  80.  
  81.     hash = (zapis **) malloc (sizeof (struct zapis *) * M);
  82.     Isprazni (hash, M);
  83.  
  84.     printf ("Upisite imena koja zelite staviti u hash tablicu.\n");
  85.     printf ("Ta kraj upisite EOL (end of list):\n");
  86.     for ever {
  87.         element = (zapis *) malloc (sizeof (zapis));
  88.         gets (element -> ime);
  89.         if (strcmp(element -> ime, "EOL") == 0){
  90.             break;
  91.         }
  92.         Upis (hash, element);
  93.     }
  94.     printf ("Upisite ime koje zelite pronaci u hashu (\"End\" za prestanak trazenja):\n");
  95.     for ever{
  96.         gets (element -> ime);
  97.         if (strcmp (element -> ime, "End") == 0)
  98.             break;
  99.         find = Trazi (hash, element);
  100.         if (find == 1)
  101.             printf ("   ->Ime je pronadeno u hashu.\n");
  102.         else
  103.             printf ("   ->Ime nije pronadeno u hashu.\n");
  104.     }
  105.  
  106.     Ispis (hash, M);
  107.  
  108.     Obrisi_Hash (hash, M);
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement