Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node
  4. {
  5. int val;
  6. struct node *next;
  7. } NodeT;
  8.  
  9. int hFunction(int key) //H=dispersie in engleza
  10. {
  11. return key%M;
  12. }
  13.  
  14. void insert(NodeT* hTable[M], int key)
  15. {
  16. int numarCasuta=hFunction(key);
  17.  
  18. NodeT* nod=(NodeT*)malloc(sizeof(NodeT));
  19. nod->val=key;
  20. nod->next=hTable[numarCasuta];//vechiul first anterior
  21. //legam noul nod de vechiul prim element anterior
  22.  
  23.  
  24. hTable[numarCasuta]=nod; //36 primul element inserat, anterior
  25.  
  26.  
  27. }
  28.  
  29.  
  30. NodeT* search(NodeT* hTable[M], int key)
  31. {
  32. int numarCasuta=hFunction(key);
  33. NodeT* p=hTable[numarCasuta]; //parcurgem lista de la acea casuta
  34.  
  35. while(p!=NULL)
  36. if(p->val==key)
  37. {
  38. //returnam elementul din lista
  39. return p;
  40. }
  41. p=p->next;
  42.  
  43. return NULL;
  44.  
  45. }
  46.  
  47.  
  48. void deleteKey(NodeT* hTable[M], int key)
  49. {
  50.  
  51. }
  52.  
  53. //afisarea tuturor elmentelor din tebela de dispersie
  54. void showAll(NodeT* hTable[M])
  55. {
  56. int i;
  57. for(i = 0; i < M; i++)
  58. {
  59. printf(" %d :",i);
  60. //verificam daca la slotul i am adaugat ceva
  61. if(hTable[i] != NULL) //{
  62. {
  63.  
  64. NodeT *p;
  65. p = hTable[i];
  66. while (p != NULL) //afisarea listei!
  67. {
  68. printf(" %d ",p->val);
  69. p = p->next;
  70. }
  71. } //}
  72. printf("\n");
  73. }
  74. printf("\n");
  75. }
  76.  
  77.  
  78. int main()
  79. {
  80.  
  81. //initializare
  82. NodeT* hTable[M];
  83. for(int i = 0; i < M; i++)
  84. {
  85. hTable[i] = NULL;
  86. }
  87.  
  88. //test inserare
  89. int vals[] = {36, 18, 6, 43, 72, 10, 5, 15};
  90. for(int i=0; i<sizeof(vals)/sizeof(int); i++)
  91. insert(hTable, vals[i]);
  92. showAll(hTable);
  93.  
  94. // 0:
  95. // 1: 15 43 36
  96. // 2: 72
  97. // 3: 10
  98. // 4: 18
  99. // 5: 5
  100. // 6: 6
  101.  
  102. //test search
  103. //test search
  104. NodeT* cautare43 = search(hTable, 43);
  105. if (cautare43 != NULL) {
  106. printf("%d a fost gasit!\n", cautare43->val);
  107. }
  108. NodeT* cautare7 = search(hTable, 7);
  109. if (cautare7 != NULL) {
  110. printf("%d a fost gasit!\n", cautare7->val);
  111. } else {
  112. printf("7 nu a fost gasit\n");
  113. }
  114.  
  115.  
  116. //test stergere
  117. int todel[] = {43, 36, 10, 61, -5};
  118. for(int i=0; i<sizeof(todel)/sizeof(int); i++)
  119. deleteKey(hTable, todel[i]);
  120. showAll(hTable);
  121.  
  122. // 0:
  123. // 1: 15
  124. // 2: 72
  125. // 3:
  126. // 4: 18
  127. // 5: 5
  128. // 6: 6
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement