Advertisement
catalin_stefann11

HASHTABLE

Jul 1st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include<iostream>
  4. #include<malloc.h>
  5. using namespace std;
  6.  
  7. struct Telefon
  8. {
  9.     char* serie;
  10.     float pret;
  11.     int memorie;
  12. };
  13.  
  14. struct nod
  15. {
  16.     Telefon info;
  17.     nod* next;
  18. };
  19.  
  20. struct Hash
  21. {
  22.     nod** vector;
  23.     int dim;
  24. };
  25.  
  26. Telefon creareTelefon(char* serie, float pret, int memorie) {
  27.     Telefon t;
  28.     t.serie = (char*)malloc(sizeof(char)*(strlen(serie) + 1));
  29.     strcpy(t.serie, serie);
  30.     t.pret = pret;
  31.     t.memorie = memorie;
  32.     return t;
  33. }
  34.  
  35. void afisareTelefon(Telefon t) {
  36.     printf("Telefonul cu seria %s costa %5.2f si are %d Gb memorie.\n", t.serie, t.pret, t.memorie);
  37. }
  38.  
  39. nod* creareNod(Telefon info, nod* next) {
  40.     nod* nodNou = (nod*)malloc(sizeof(nod));
  41.     nodNou->info = creareTelefon(info.serie, info.pret, info.memorie);
  42.     nodNou->next = next;
  43.  
  44.     return nodNou;
  45. }
  46.  
  47. nod* inserareSfarsit(nod*cap, Telefon t)
  48. {
  49.     nod* nou = creareNod(t, NULL);
  50.     if (cap)
  51.     {
  52.         nod*p = cap;
  53.         while (p->next)
  54.         {
  55.             p = p->next;
  56.         }
  57.         p->next = nou;
  58.         return cap;
  59.     }
  60.     else
  61.     {
  62.         return nou;
  63.     }
  64. }
  65.  
  66. int functionHash(char* serie, int dim)
  67. {
  68.     int s = 0;
  69.     for (int i = 0; i < strlen(serie); i++)
  70.     {
  71.         s += serie[i];
  72.     }
  73.     return s%dim;
  74. }
  75.  
  76. Hash initializareHash(int dim)
  77. {
  78.     Hash hash;
  79.     hash.dim = dim;
  80.     hash.vector = (nod**)malloc(sizeof(nod*)*dim);
  81.     for (int i = 0; i < dim; i++)
  82.     {
  83.         hash.vector[i] = NULL;
  84.     }
  85.     return hash;
  86. }
  87.  
  88. int inserareInTabela(Telefon t, Hash hs)
  89. {
  90.     if (hs.vector)
  91.     {
  92.         int hashCode = functionHash(t.serie, hs.dim);
  93.         if (hs.vector[hashCode])
  94.         {
  95.             hs.vector[hashCode] = inserareSfarsit(hs.vector[hashCode], t);
  96.             return hashCode;
  97.         }
  98.         else
  99.         {
  100.             hs.vector[hashCode] = creareNod(t, NULL);
  101.             return hashCode;
  102.         }
  103.     }
  104.     else
  105.     {
  106.         return -1;
  107.     }
  108. }
  109.  
  110. void afisareLista(nod* cap)
  111. {
  112.     if (cap)
  113.     {
  114.         nod* p = cap;
  115.         while (p)
  116.         {
  117.             afisareTelefon(p->info);
  118.             p = p->next;
  119.         }
  120.     }
  121. }
  122.  
  123. void afisareHash(Hash hs)
  124. {
  125.     if (hs.vector)
  126.     {
  127.         for (int i = 0; i < hs.dim; i++)
  128.         {
  129.             nod*p = hs.vector[i];
  130.             if (hs.vector[i])
  131.             {
  132.                 printf("pozitia: %d\n", i);
  133.                 afisareLista(p);
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. Telefon cautareDupaSerie(char*serie, Hash ht)
  141. {
  142.     if (ht.vector)
  143.     {
  144.         int pozitie = functionHash(serie, ht.dim);
  145.         nod* p = ht.vector[pozitie];
  146.         while (p&&strcmp(serie, p->info.serie) != 0)
  147.         {
  148.             p = p->next;
  149.         }
  150.         if (p)
  151.         {
  152.             return p->info;
  153.         }
  154.  
  155.     }
  156.     Telefon t;
  157.     t.serie = NULL;
  158.     t.pret = -1;
  159.     t.memorie = -1;
  160.     return t;
  161. }
  162.  
  163. void stergereSpatiu(Hash *hash)
  164. {
  165.     for (int i = 0; i < hash->dim; i++)
  166.     {
  167.         nod* p = hash->vector[i];
  168.         while (p)
  169.         {
  170.             free(p->info.serie);
  171.             nod* aux = p;
  172.             p = p->next;
  173.             free(aux);
  174.         }
  175.         hash->vector[i] = NULL;
  176.     }
  177.     free(hash->vector);
  178.     hash->dim = 0;
  179.     hash->vector = NULL;
  180. }
  181.  
  182. void main()
  183. {
  184.     Hash hash;
  185.     hash = initializareHash(6);
  186.     int pozitie = inserareInTabela(creareTelefon("C45D3", 1000, 4), hash);
  187.     inserareInTabela(creareTelefon("D34C5", 1200, 6), hash);
  188.     inserareInTabela(creareTelefon("F36D5", 1700, 12), hash);
  189.  
  190.     afisareHash(hash);
  191.  
  192.     Telefon t = cautareDupaSerie("F36D5", hash);
  193.     if (t.serie)
  194.     {
  195.         afisareTelefon(t);
  196.     }
  197.  
  198.     stergereSpatiu(&hash);
  199.     afisareHash(hash);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement