Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define MAX 1000
  8. #define     MEMCHECK(x) if(x==NULL) { printf("Nema memorija!\n"); exit(-1); }
  9.  
  10. typedef char * key_struct;
  11. typedef char * value_struct;
  12.  
  13. typedef struct hashmap_element
  14. {
  15.     key_struct key;                      
  16.     value_struct value; } hashmap_node;                
  17.  
  18. typedef struct element
  19. {
  20.     hashmap_node info;
  21.     struct element *link;
  22. } h_node;
  23. typedef h_node * h_nodep;
  24.  
  25. typedef struct cbht
  26. {
  27.     int size;
  28.     h_node ** buckets;
  29. } hashmap;                    
  30.  
  31. void make_hash_map(hashmap *tptr, int n)  
  32. {
  33.     int i;
  34.     tptr->size=n;
  35.     tptr->buckets=(h_node **) calloc(tptr->size, sizeof(h_node *));
  36.  
  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';
  45. }
  46.  
  47. int hash(key_struct key, hashmap *tptr)
  48. {
  49.         return hashCode(key)%(tptr->size);
  50.  
  51. }
  52.  
  53. h_node * search(key_struct targetKey, hashmap *tptr)
  54. {
  55.     int h;
  56.     h_node *p;
  57.     h=hash(targetKey, tptr);
  58.     for(p=tptr->buckets[h]; p!=NULL; p=p->link)
  59.     {
  60.         if (!strcmp((p->info).key, targetKey))
  61.             return p;
  62.     }
  63.     return NULL;
  64. }
  65.  
  66.  
  67.  
  68. void insert(key_struct key, value_struct val, hashmap *tptr)
  69. {
  70.     h_node *tmp;
  71.     h_node *p;
  72.     int h;
  73.  
  74.     h=hash(key, tptr);
  75.     p=(h_node *) malloc(sizeof(h_node)); (p->info).value=val;
  76.     (p->info).key=key;
  77.    
  78.     for(tmp=tptr->buckets[h]; tmp!=NULL; tmp=tmp->link)
  79.     {        
  80. if (!strcmp((tmp->info).key, key))
  81.         {
  82.             (tmp->info).value=val; return;
  83.         }
  84.     }
  85.    
  86. p->link=tptr->buckets[h];
  87.     tptr->buckets[h]=p;
  88. }
  89.  
  90. void delete(key_struct key, hashmap *tptr)
  91. {
  92.     h_node *p, *last;
  93.     int h;
  94.  
  95.     h=hash(key,tptr);
  96.     for (p=tptr->buckets[h]; p!=NULL; p=p->link)
  97.     {
  98.         if (!strcmp((p->info).key, key))
  99.             break;
  100.     }
  101.     if (p==NULL) return;  
  102.  
  103.     if(p==tptr->buckets[h])  
  104.         tptr->buckets[h]=p->link;
  105.     else
  106.     {
  107. for (last=tptr->buckets[h]; (last!=NULL)&&(last->link!=NULL); last=last->link)
  108.         {
  109.             if (last->link==p)
  110.                 break;
  111.         }
  112.         last->link=p->link;
  113.     }
  114.  
  115.     free(p); }
  116.  
  117. void print_h_node(h_node *p)
  118. {
  119.     printf("(%s,%s)",(p->info).key,(p->info).value);
  120. }
  121.  
  122. void print_hashmap(hashmap *tptr)
  123. {
  124.     int i;
  125.     h_node *p;
  126.    
  127.     for(i=0; i<tptr->size; i++)
  128.     {
  129.         printf("%d:",i);
  130.         for(p=tptr->buckets[i]; p!=NULL; p=p->link)
  131.             print_h_node(p);
  132.         printf("\n");
  133.     }
  134. }
  135.  
  136. int main()
  137. {
  138.     hashmap *hashTabela=(hashmap *)malloc(sizeof(hashmap));
  139.     int N,i;
  140.     scanf("%d",&N);
  141.    
  142.     make_hash_map(hashTabela,2*N+1);
  143.  
  144.     char user[100][30];
  145.     char pass[100][30];
  146.     int h;
  147.  
  148.     for(i=0; i<N; i++)
  149.     {
  150.         scanf("%s",user[i]);
  151.         scanf("%s",pass[i]);
  152.         insert(user[i],pass[i],hashTabela);
  153.     }
  154.  
  155.     char chusername[100][100];
  156.     char chlozinka[100][100];
  157.     i=0;
  158.     while(scanf("%s", chusername[i])){
  159.         if(!strcmp("KRAJ",chusername[i]))
  160.             break;
  161.         scanf("%s", chlozinka[i]);
  162.         i++;
  163.     }
  164.    
  165.    
  166.    
  167.     int j;
  168.     for(j=0; j<i; j++)
  169.     {
  170.         int flg = 0;
  171.         h_node * tmp = search(chusername[j], hashTabela);      
  172.        if (tmp == NULL)
  173.         {
  174.             printf("Nenajaven\n");
  175.         }
  176.         else
  177.         {
  178.             if (!strcmp(tmp->info.value,chlozinka[j]))
  179.             {
  180.                 printf("Najaven\n");
  181.                 flg = 1;
  182.             }
  183.             else printf("Nenajaven\n");
  184.         }
  185.         if (flg) break;
  186.     }  
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement