Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define MAX 1000
  6. #define MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
  7.  
  8. typedef char * key_struct;
  9. typedef char * value_struct;
  10.  
  11. typedef struct hashmap_element
  12. {
  13. key_struct key; // kluc
  14. value_struct value; // vrednost
  15. } hashmap_node; // Definiranje element vo hash mapa
  16.  
  17. typedef struct element
  18. {
  19. hashmap_node info;
  20. struct element *link;
  21. } h_node; // Definiranje na element vo linearna lista (koficka) vo hash mapa
  22.  
  23. typedef h_node * h_nodep;
  24.  
  25. typedef struct cbht
  26. {
  27. int size;
  28. h_node ** buckets; // niza od koficki (linearni listi)
  29. } hashmap; // Definiranje na hash mapa
  30.  
  31. void make_hash_map(hashmap *tptr, int n) // inicijalizacija na hash mapa
  32. {
  33. int i;
  34. tptr->size=n;
  35. tptr->buckets=(h_node **) calloc(tptr->size, sizeof(h_node *));
  36. // alokacija na memorija za nizata od koficki
  37.  
  38. for(i=0;i<tptr->size;i++)
  39. tptr->buckets[i]=NULL;
  40. }
  41.  
  42. int hashCode(key_struct key)
  43. {
  44. return key[0]-'a'; // funkcija koja naoga hash kod na char *
  45. // za drugi tipovi na klucevi treba da se definira druga hashCode funkcija
  46. }
  47.  
  48. int hash(key_struct key, hashmap *tptr)
  49. {
  50. // funkcija koja presmetuva hash
  51. return hashCode(key)%(tptr->size);
  52.  
  53. }
  54.  
  55. h_node * search(key_struct targetKey, value_struct targetVal, hashmap *tptr)
  56. // funkcija za prebaruvanje vo hash
  57. {
  58. int h;
  59. h_node *p;
  60. h=hash(targetKey, tptr);
  61. for(p=tptr->buckets[h]; p!=NULL; p=p->link)
  62. {
  63. if (!strcmp((p->info).key, targetKey))
  64. {
  65. if (!strcmp((p->info).value, targetVal))
  66. return p;
  67. else
  68. return NULL;
  69. }
  70. }
  71. return NULL;
  72. }
  73.  
  74.  
  75.  
  76. void insert(key_struct key, value_struct val, hashmap *tptr)
  77. // funkcija za vnesuvanje vo hash
  78. {
  79. h_node *tmp;
  80. h_node *p;
  81. int h;
  82.  
  83. h=hash(key, tptr);
  84. p=(h_node *) malloc(sizeof(h_node)); // alokacija za noviot jazel
  85. (p->info).value=val;
  86. (p->info).key=key;
  87.  
  88. for(tmp=tptr->buckets[h]; tmp!=NULL; tmp=tmp->link)
  89. {
  90. // ako veke postoi takov zapis
  91. if (!strcmp((tmp->info).key, key))
  92. {
  93. (tmp->info).value=val; // se zamenuva so novata vrednost
  94. return;
  95. }
  96. }
  97.  
  98. // se vnesuva noviot jazel na pocetokot na kofickata h
  99. p->link=tptr->buckets[h];
  100. tptr->buckets[h]=p;
  101. }
  102.  
  103. void delete(key_struct key, hashmap *tptr)
  104. // funkcija za brisenje na onie jazli so ist kluc kako dadeniot
  105. {
  106. h_node *p, *last;
  107. int h;
  108.  
  109. h=hash(key,tptr);
  110. for (p=tptr->buckets[h]; p!=NULL; p=p->link)
  111. {
  112. if (!strcmp((p->info).key, key))
  113. break;
  114. }
  115. if (p==NULL) return; // ako nema takvi jazli za brisenje
  116.  
  117. if(p==tptr->buckets[h]) // ako najdeniot e na pocetok na koficka za brisenje
  118. tptr->buckets[h]=p->link;
  119. else
  120. {
  121. // naoganje na prethodnikot na jazelot sto treba da se izbrise
  122. for (last=tptr->buckets[h]; (last!=NULL)&&(last->link!=NULL); last=last->link)
  123. {
  124. if (last->link==p)
  125. break;
  126. }
  127. last->link=p->link;
  128. }
  129.  
  130. free(p); // brisenje na jazel
  131. }
  132.  
  133. void print_h_node(h_node *p)
  134. {
  135. printf("(%s,%s)",(p->info).key,(p->info).value);
  136. }
  137.  
  138. void print_hashmap(hashmap *tptr)
  139. {
  140. int i;
  141. h_node *p;
  142.  
  143. for(i=0; i<tptr->size; i++)
  144. {
  145. printf("%d:",i);
  146. for(p=tptr->buckets[i]; p!=NULL; p=p->link)
  147. print_h_node(p);
  148. printf("\n");
  149. }
  150. }
  151.  
  152. int main()
  153. {
  154. hashmap *hashTabela=(hashmap *)malloc(sizeof(hashmap));
  155. int N,i;
  156. scanf("%d",&N);
  157. //--Sami ja postavuvate goleminata na hesh tabelata
  158. make_hash_map(hashTabela,26);
  159.  
  160. char user[100][30];
  161. char pass[100][30];
  162. int h, broj_vneseni=0;
  163.  
  164. for(i=0; i<N; i++)
  165. {
  166. scanf("%s",user[i]);
  167. scanf("%s",pass[i]);
  168. insert(user[i],pass[i],hashTabela);
  169. } //vnesuvanje na userite i lozinkite vo hash
  170.  
  171. char chusername[100][100];
  172. char chlozinka[100][100];
  173. i=0;
  174. while(scanf("%s", chusername[i])){
  175. if(!strcmp("KRAJ",chusername[i]))
  176. break;
  177. scanf("%s", chlozinka[i]);
  178. i++;
  179. }
  180. broj_vneseni=i;
  181. h_node *bara;
  182. int flag=0;
  183. for(i=0; i<broj_vneseni-1; i++)
  184. {
  185. bara=search(chusername[i],chlozinka[i],hashTabela);
  186. if(bara==NULL)
  187. {
  188. if(flag!=1)
  189. printf("Nenajaven\n");
  190. else break;
  191. }
  192. else
  193. {
  194. printf("Najaven");
  195. flag=1;
  196. }
  197. }
  198. bara=search(chusername[i],chlozinka[i],hashTabela);
  199. if(flag==0)
  200. {
  201. if(bara==NULL)
  202. printf("Nenajaven");
  203. else
  204. printf("Najaven");
  205. }
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement