Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include "stdafx.h"
  5. #include<stdio.h>
  6. #include<malloc.h>
  7. #include<string>
  8. #include<stdlib.h>
  9. using namespace std;
  10.  
  11. struct Prajitura
  12. {
  13.     int cod;
  14.     char* ingredientPrincipal;
  15.     float numarCalorii;
  16. };
  17.  
  18. Prajitura citirePrajitura(int c, char* ingr, float numar)
  19. {
  20.     Prajitura p;
  21.     p.cod = c;
  22.     p.ingredientPrincipal = (char*)malloc((strlen(ingr) + 1) * sizeof(char));
  23.     strcpy(p.ingredientPrincipal, ingr);
  24.     p.numarCalorii = numar;
  25.     return p;
  26. }
  27.  
  28. void afisarePrajitura(Prajitura p)
  29. {
  30.     printf("\n Cod: %d -> Prajitura cu %s are %5.2f calorii. ",p.cod,p.ingredientPrincipal,p.numarCalorii);
  31. }
  32.  
  33. struct nod
  34. {
  35.     Prajitura info;
  36.     nod* next;
  37. };
  38.  
  39. nod* creareNod(nod* next, Prajitura praji)
  40. {
  41.     nod* nou = (nod*)malloc(sizeof(nod));
  42.     nou->info = citirePrajitura(praji.cod, praji.ingredientPrincipal, praji.numarCalorii);
  43.     nou->next = next;
  44.     return nou;
  45. }
  46.  
  47. nod* inserareInceput(nod* cap, Prajitura praji)
  48. {
  49.     return creareNod(cap, praji);
  50. }
  51.  
  52. struct HashTable
  53. {
  54.     nod** vector;
  55.     int dim;
  56. };
  57.  
  58. int getHashCode(int cod, int dim)
  59. {
  60.     return cod % dim;
  61. }
  62.  
  63. HashTable initializareHashTable(int dim)
  64. {
  65.     HashTable tabela;
  66.     tabela.dim = dim;
  67.     tabela.vector = (nod**)malloc(dim * sizeof(nod*));
  68.  
  69.     for (int i = 0; i < dim; i++)
  70.     {
  71.         tabela.vector[i] = NULL;
  72.     }
  73.     return tabela;
  74. }
  75.  
  76. int inserareInHashTable(HashTable tabela, Prajitura praji)
  77. {
  78.     if (tabela.vector)
  79.     {
  80.         int hashCode = getHashCode(tabela.dim, praji.cod);
  81.         if (tabela.vector[hashCode])
  82.         {
  83.             tabela.vector[hashCode] = inserareInceput(tabela.vector[hashCode], praji);
  84.         }
  85.         else
  86.         {
  87.             tabela.vector[hashCode] = creareNod(NULL, praji);
  88.         }
  89.         return hashCode;
  90.     }
  91.     else
  92.     {
  93.         return -1;
  94.     }
  95. }
  96.  
  97. void afisareTabela(HashTable tabela)
  98. {
  99.     for (int i = 0; i < tabela.dim; i++)
  100.     {
  101.         nod* p = tabela.vector[i];
  102.         while (p)
  103.         {
  104.             afisarePrajitura(p->info);
  105.             p = p->next;
  106.         }
  107.         printf("\n");
  108.     }
  109. }
  110.  
  111. void stergereTabela(HashTable* tabela)
  112. {
  113.     for (int i = 0; i < tabela->dim;i++)
  114.     {
  115.         nod* p = tabela->vector[i];
  116.         while (p)
  117.         {
  118.             free(p->info.ingredientPrincipal);
  119.             nod* temp = p;
  120.             p = p->next;
  121.             free(temp);
  122.         }
  123.     }
  124.     free(tabela->vector);
  125.     tabela->vector = NULL;
  126.     tabela->dim = 0;
  127. }
  128.  
  129. int main()
  130. {
  131.     HashTable tabela = initializareHashTable(5);
  132.  
  133.     int pozitie = inserareInHashTable(tabela, citirePrajitura(1,(char*)"Lapte",30));
  134.     inserareInHashTable(tabela, citirePrajitura(4, (char*)"Faina", 100));
  135.     inserareInHashTable(tabela, citirePrajitura(6, (char*)"Ciocolata", 200));
  136.  
  137.     afisareTabela(tabela);
  138.  
  139.  
  140.  
  141.  
  142.     stergereTabela(&tabela);
  143.  
  144.  
  145.     getchar();
  146.     getchar();
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement