Advertisement
Ladies_Man

3_12 Dispersed Array

Jan 23rd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int m;
  5. #define hash_func(x) ((x) % m)
  6.  
  7. char t_at[3] = "AT";
  8. char t_as[7] = "ASSIGN";
  9. int i, n, ind = 1, j = 0;
  10.  
  11.  
  12.  
  13. struct hash {
  14.     struct hash *next;
  15.     int key;
  16.     int value;
  17. } node;
  18.  
  19. struct hash **array;
  20.  
  21.  
  22.  
  23. void inithash (struct hash *s)
  24. {
  25.     i = 0;
  26.     array = (struct hash**)malloc(m * sizeof (struct hash*));
  27.     while (i < m) {
  28.         array[i] = NULL;
  29.         i++;
  30.     }
  31. }
  32.  
  33.  
  34.  
  35. void find (int val)
  36. {
  37.     struct hash *turn;
  38.     turn = array [hash_func(val)];
  39.     //skip (turn, val);
  40.     //std_chk(val);
  41.     while (turn != NULL && turn->key != val) turn = turn->next;
  42.     if (NULL != turn) printf("%d\n", turn->value);
  43.     else printf("0\n");
  44. }
  45.  
  46.  
  47.  
  48. void delete (int k, int v)
  49. {
  50.     struct hash *dummy, *dummy2;
  51.     dummy = NULL;
  52.     dummy2 = array[hash_func(k)];
  53.     if (NULL != dummy2 && k != dummy2) {
  54.         dummy = dummy2;
  55.         dummy2 = dummy2->next;
  56.         return;
  57.     }
  58.     if (NULL != dummy2 && k == dummy2) {
  59.         if (NULL == dummy) {
  60.             dummy = array[hash_func(k)];
  61.             array[hash_func(k)] = dummy->next;
  62.             free(dummy);
  63.             return;
  64.         }
  65.         dummy2 = dummy->next;
  66.         dummy->next = dummy2->next;
  67.         free(dummy2);
  68.     }
  69. }
  70.  
  71.  
  72.  
  73. void insert (int k, int v)
  74. {
  75.     struct hash *turn, *dummy;
  76.     turn = array[hash_func(k)];
  77.     void indumm (int key, int value) {
  78.         dummy = malloc(ind * sizeof(struct hash));
  79.         dummy->key = k;
  80.         dummy->value = v;
  81.         dummy->next = array[hash_func(k)];
  82.         array[hash_func(k)] = dummy;
  83.     }
  84.     //skip (turn, v);
  85.     //std_chk(v);
  86.     while (turn != NULL && turn->key != k) turn = turn->next;
  87.  
  88.     if (0 == v) delete (k, v);
  89.     if (0 != v && NULL != turn) turn->value = v;
  90.     else indumm (k, v);
  91. }
  92.  
  93.  
  94.  
  95. void mem_salvation (void)
  96. {
  97.     struct hash *dummy;
  98.     for (i = 0; i < m; i++) {
  99.         if (NULL != array[i]) {
  100.             while (array[i] != NULL) {
  101.                 dummy = array[i];
  102.                 array[i] = array[i]->next;
  103.                 free(dummy);
  104.             }
  105.         }
  106.     }
  107.     free(array);
  108.     //printf(">memfreed\n");
  109. }
  110.  
  111.  
  112.  
  113. int main()
  114. {
  115.     struct hash turn;
  116.     int value_a, value_b, value_c, i;
  117.     char cmm[7] = { 0 };
  118.     scanf("%d", &n);
  119.     scanf("%d", &m);
  120.  
  121.     inithash(&turn);
  122.  
  123.     for (i = 0; i < n; i++) {
  124.         scanf("%s", cmm);
  125.         if (0 == strcmp(cmm, t_at)) {
  126.             scanf ("%d", &value_a);
  127.             find (value_a);
  128.             continue;
  129.         }
  130.         if (0 == strcmp(cmm, t_as)) {
  131.             scanf ("%d %d", &value_b, &value_c);
  132.             insert (value_b, value_c);
  133.         }
  134.     }
  135.  
  136.     mem_salvation();
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement