Advertisement
Misipuk

KP_2

Apr 29th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <time.h>
  5.  
  6. #define N 227
  7. #define N1 100
  8. #define MAX 31000
  9.  
  10. int compare = 0;
  11. int swap = 0;
  12.  
  13. int minCompare = 1000, maxCompare = 0, minSwap = 1000, maxSwap = 0, midCompare = 0, midSwap = 0;
  14.  
  15. typedef struct{
  16.     int key;
  17. }item;
  18.  
  19. typedef struct{
  20.     int n;
  21.     item ** array;
  22. }DAHashTable;
  23.  
  24. typedef struct MyList{
  25.     item * it;
  26.     struct MyList * next;
  27.     struct MyList * prev;
  28. }List;
  29.  
  30.  
  31. typedef struct{
  32.     int n;
  33.     List ** array;
  34. }CHHashTable;
  35.  
  36. void DA_INIT(DAHashTable * T);
  37. int DA_INSERT(DAHashTable * T, item * it);
  38. void DA_DELETE(DAHashTable * T, item * it);
  39. item * DA_SEARCH(DAHashTable * T, int key);
  40.  
  41. void CH_INIT(CHHashTable * T);
  42. void CH_INSERT(CHHashTable * T, item * it);
  43. void CH_DELETE(CHHashTable * T, item * it);
  44. item * CH_SEARCH(CHHashTable * T, int key);
  45. List* addToList(List * head, item * it);
  46. List* deleteFromList(List * head, item * it);
  47. item * searchInList(List * head, int key);
  48.  
  49. int h(int key);
  50. void calculateCS();
  51. void displayCS();
  52.  
  53.  
  54.  
  55. int main()
  56. {
  57.     srand(time(0));
  58.     SetConsoleCP(1251);
  59.     SetConsoleOutputCP(1251);
  60.     int mas[N], i;
  61.     //Пряма адресація
  62.     DAHashTable * daT = (DAHashTable *)malloc(sizeof(DAHashTable));
  63.     DA_INIT(daT);
  64.     int sizeT = sizeof(DAHashTable) + sizeof(item[MAX]);
  65.     for(i = 0; i < N; i++){
  66.         item * it = (item *)malloc(sizeof(item));
  67.         while(1){
  68.             mas[i] = rand() % MAX;
  69.             it->key = mas[i];
  70.             if(DA_INSERT(daT, it))break;
  71.         }
  72.         calculateCS();
  73.     }
  74.     printf("Direct adressing:\nHash table size: %d байт; Extra memory 0 байт\nAdding:\n", sizeT);
  75.     displayCS();
  76.  
  77.     for(i = 0; i < N; i++){
  78.         item * it = DA_SEARCH(daT, mas[i]);
  79.         calculateCS();
  80.     }
  81.     printf("Search:\n");
  82.     displayCS();
  83.  
  84.     for(i = 0; i < N; i++){
  85.         item * it = DA_SEARCH(daT, mas[i]);
  86.         DA_DELETE(daT, it);
  87.  
  88.         calculateCS();
  89.     }
  90.     printf("Delete:\n");
  91.     displayCS();
  92.  
  93.     //Метод Ланцюгів
  94.     CHHashTable * chT = (CHHashTable *)malloc(sizeof(CHHashTable));
  95.     CH_INIT(chT);
  96.     sizeT = sizeof(CHHashTable)+ sizeof(List)*N + sizeof(item)*N;///SIZE
  97.     int sizeD = sizeof(List)*N;
  98.     for(i = 0; i < N; i++){
  99.         item * it = (item *)malloc(sizeof(item));
  100.         mas[i] = rand() % MAX;
  101.         it->key = mas[i];
  102.         CH_INSERT(chT, it);
  103.         calculateCS();
  104.     }
  105.     printf("Method Lanc:\nHash table size: %d bytes;  Extra memory %d bytes\nAdded:\n", sizeT, sizeD);
  106.     displayCS();
  107.  
  108.     for(i = 0; i < N; i++){
  109.         item * it = CH_SEARCH(chT, mas[i]);
  110.         calculateCS();
  111.     }
  112.     printf("Search:\n");
  113.     displayCS();
  114.  
  115.     for(i = N-1; i >=0; i--){
  116.         item * it = CH_SEARCH(chT, mas[i]);
  117.         CH_DELETE(chT, it);
  118.  
  119.         calculateCS();
  120.     }
  121.     printf("Delete:\n");
  122.     displayCS();
  123.  
  124.  
  125.     //Метод Ланцюгів 100
  126.     CHHashTable * chTN = (CHHashTable *)malloc(sizeof(CHHashTable));
  127.     CH_INIT(chTN);
  128.     sizeT = sizeof(CHHashTable)+ sizeof(List)*N1 + sizeof(item)*N1;///SIZE
  129.     sizeD = sizeof(List)*N1;
  130.     for(i = 0; i < N1; i++){
  131.         item * it = (item *)malloc(sizeof(item));
  132.         mas[i] = rand() % MAX;
  133.         it->key = mas[i];
  134.         CH_INSERT(chTN, it);
  135.         calculateCS();
  136.     }
  137.     printf("Method Lanc 100:\nHash table size: %d bytes;  Extra memory %d bytes\nAdded:\n", sizeT, sizeD);
  138.     displayCS();
  139.  
  140.     for(i = 0; i < N1; i++){
  141.         item * it = CH_SEARCH(chTN, mas[i]);
  142.         calculateCS();
  143.     }
  144.     printf("Search:\n");
  145.     displayCS();
  146.  
  147.     for(i = N1-1; i >=0; i--){
  148.         item * it = CH_SEARCH(chTN, mas[i]);
  149.         CH_DELETE(chTN, it);
  150.  
  151.         calculateCS();
  152.     }
  153.     printf("Delete:\n");
  154.     displayCS();
  155.     return 0;
  156. }
  157.  
  158.  
  159. //З прямою адресацією
  160. void DA_INIT(DAHashTable * T){
  161.     T->n=MAX;
  162.     T->array =(item *)malloc(sizeof(item[T->n]));
  163.     int i;
  164.     for(i = 0; i < T->n; i++){
  165.         T->array[i]=NULL;
  166.     }
  167. }
  168. item * DA_SEARCH(DAHashTable * T, int key){
  169.     return T->array[key];
  170. }
  171.  
  172. int DA_INSERT(DAHashTable * T, item * it){
  173.     compare++;
  174.     if(T->array[it->key] == NULL){
  175.         swap++;
  176.         T->array[it->key] = it;
  177.         return 1;
  178.     }
  179.     return 0;
  180. }
  181. void DA_DELETE(DAHashTable * T, item * it){
  182.     swap++;
  183.     T->array[it->key] = NULL;
  184. }
  185.  
  186. void CH_INIT(CHHashTable * T){
  187.     T->n=N;
  188.     T->array =(List *)malloc(sizeof(List[T->n]));
  189.     int i;
  190.     for(i = 0; i < T->n; i++){
  191.         T->array[i]=NULL;
  192.     }
  193. }
  194. item * CH_SEARCH(CHHashTable * T, int key){
  195.     return searchInList(T->array[h(key)], key);
  196. }
  197.  
  198. void CH_INSERT(CHHashTable * T, item * it){
  199.     swap++;
  200.     T->array[h(it->key)] = addToList(T->array[h(it->key)], it);
  201. }
  202. void CH_DELETE(CHHashTable * T, item * it){
  203.     swap++;
  204.     T->array[h(it->key)] = deleteFromList(T->array[h(it->key)], it);
  205. }
  206.  
  207. int h(int it){
  208.     return it % N;
  209. }
  210.  
  211.  
  212. List * addToList(List * head, item * it){
  213.     List * tmp = head;
  214.     List * list = (List *)malloc(sizeof(List));
  215.     swap+=2;
  216.     list->it = it;
  217.     list->next = NULL;
  218.     compare++;
  219.     if(head == NULL){
  220.         swap+=2;
  221.         head = list;
  222.         list->prev = NULL;
  223.     }
  224.     else{
  225.         while(tmp->next != NULL){
  226.             compare++;
  227.             swap++;
  228.             tmp = tmp->next;
  229.  
  230.         }
  231.         compare++;
  232.         swap+=2;
  233.         tmp->next = list;
  234.         list->prev = tmp;
  235.     }
  236.     return head;
  237. }
  238. List* deleteFromList(List * head, item * itm){
  239.     List * tmp, * temp = head;
  240.     compare++;
  241.     if(head->it->key == itm->key){
  242.         swap+=3;
  243.         tmp = head;
  244.         head = head->next;
  245.         if(head != NULL)head->prev = NULL;
  246.         free(tmp);
  247.     }
  248.     else{
  249.         while(temp->next != NULL){
  250.             compare += 2;
  251.             if((temp->next)->it->key == itm->key){
  252.                 swap+=3;
  253.                 tmp = temp->next;
  254.                 if (tmp->next!=NULL) tmp->next->prev = temp;//???
  255.                 temp->next = tmp->next;
  256.                 free(tmp);
  257.                 break;
  258.             }
  259.             swap++;
  260.             temp = temp->next;
  261.         }
  262.         compare++;
  263.     }
  264.     return head;
  265. }
  266. item * searchInList(List * head, int key){
  267.     while(head != NULL){
  268.         compare+=2;
  269.         if(head->it->key == key){
  270.             return head->it;
  271.         }
  272.         head = head->next;
  273.         swap++;
  274.  
  275. //        printf("%d ", head->it->key);
  276.     }
  277.     compare++;
  278.     return NULL;
  279. }
  280.  
  281. void calculateCS(){
  282.     midCompare += compare;
  283.     if(minCompare > compare)minCompare = compare;
  284.     if(maxCompare < compare)maxCompare = compare;
  285.  
  286.     midSwap += swap;
  287.     if(minSwap > swap)minSwap = swap;
  288.     if(maxSwap < swap)maxSwap = swap;
  289.  
  290.     compare = swap = 0;
  291. }
  292. void displayCS(){
  293.     printf("Sravn:\t min: %d,   mid: %.2f,   max: %d\n", minCompare, (double)midCompare/N, maxCompare);
  294.     printf("Copy:\t min: %d,   mid: %.2f,   max: %d\n\n", minSwap, (double)midSwap/N, maxSwap);
  295.     minCompare = minSwap = 1000;
  296.     maxCompare = maxSwap = midCompare = midSwap = 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement