Advertisement
Guest User

Set.c

a guest
Aug 7th, 2010
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include "set.h"
  4.  
  5. /**
  6.  * Set_Create: O(1)
  7.  * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
  8.  * Set.
  9. */
  10. void Set_Create(Set* set){
  11.    if(set == NULL){
  12.        set = (Set *)malloc(sizeof(Set));
  13.    }
  14.    if(set == NULL)
  15.      printf("Error");
  16.  
  17.  //  printf("Hello\n");
  18.    set->size = 0;
  19.  //  printf("Size = %d",set->size);
  20.    set->data = -1;
  21.    set->next = NULL;
  22. }
  23.  
  24. /**
  25.  * Set_Create: 0(n) where n = Size(set1)
  26.  *
  27.  * Function Used to create set2 such that it is initialized with the same values as set1
  28.  * This function is traverses set1 completely to create set2. Hence Linear Time Complexity
  29. */
  30. void Set_Create(const Set* set1, Set* set2){
  31.    Set_Create(set2);
  32.    const Set* temp = set1;
  33.  
  34.    while(temp != NULL){
  35.        set2->data = temp->data;
  36.        temp = temp->next;
  37.        set2->next = (Set *)malloc(sizeof(Set));
  38.        set2 = set2->next;
  39.    }
  40.    set2->next = NULL;
  41. }
  42.  
  43. /**
  44.  * Set_Clear: O(1)
  45.  * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
  46.  * Set.
  47. */
  48. void Set_Clear(Set* set){
  49.   Set_Create(set);
  50. }
  51.  
  52. /**
  53.  * Set_Size: O(1)
  54.  * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
  55.  * Set.
  56.  */
  57. int Set_Size(const Set* set) {
  58.    if(set == NULL){
  59.       return -1;
  60.    }
  61.  
  62.    return set->size;
  63. }
  64.  
  65. /**
  66.  * Set_Empty: O(1)
  67.  * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
  68.  * Set.
  69. */
  70. Boolean Set_Empty(const Set* set){
  71.     if(set == NULL || set->size == 0){
  72.        return TRUE;
  73.     }
  74.     return FALSE;
  75. }
  76. /**
  77.  * Set_Insert: O(n)
  78.  * This function has constant time complexity because it Traversese each element of set before inserting to make sure that no duplicate values are
  79.  * present in the set.
  80. */
  81. Boolean Set_Insert(Set* set, SetEntry value){
  82.    if(set == NULL){
  83.        printf("Set is not initialized");
  84.        return FALSE;
  85.    }
  86.  
  87.    if(set->size == 0){
  88.        set->data = value;
  89.        ++set->size;
  90.        return TRUE;
  91.    }
  92.    else {
  93.        Set* temp = set;
  94.        while(temp->next != NULL){
  95.           if(temp->data == value) //.. Can not Insert Value as it is already present in the Set
  96.              return FALSE;
  97.  
  98.           temp = temp->next;
  99.        }
  100.  
  101.        temp->next = (Set *)malloc(sizeof(Set));
  102.        temp = temp->next;
  103.        temp->data = value;
  104.        temp->next = NULL;
  105.        ++(set->size);
  106.        return TRUE;
  107.    }
  108. }
  109.  
  110. /**
  111.  * Set_Delete: O(n)
  112.  *
  113.  * This Function Traverses set entirely (in worst case) in sequential manner to find the desired value. Hence Linear Time Complexity
  114. */
  115. Boolean Set_Delete(Set* set, SetEntry value){
  116.   if(set == NULL || set->size == 0){
  117.     printf("Set Does not Exists or not initialized");
  118.      return FALSE;
  119.   }
  120.  
  121.   else{
  122.      Set* temp = set;
  123.      Set* tempprev = set;
  124.  
  125.      while(temp != NULL){
  126.         if(temp->data == value){
  127.             if(temp == set){ // if it is the first element.
  128.                 tempprev = set;
  129.                 free(set);
  130.                 set = tempprev->next;
  131.             }
  132.             else {
  133.                 tempprev->next = temp->next;
  134.             }
  135.             --(temp->size);
  136.             return TRUE;
  137.         }
  138.         tempprev = temp;
  139.         temp = temp->next;
  140.      }
  141.      printf("Set Entry to be removed not Found");
  142.      return FALSE;
  143.   }
  144. }
  145.  
  146. /**
  147.  * Set_Contains: O(n)
  148.  *
  149.  * Linear Time Complexity because to find the match, entire set has to be traversed in a sequential manner (in worst case).
  150.  *
  151. */
  152. Boolean Set_Contains(const Set* set, SetEntry value){
  153.    if(set == NULL || set->size == 0){
  154.         printf("Set Does not Exists ");
  155.         return FALSE;
  156.    }
  157.    else {
  158.       const Set* temp = set;
  159.       while(temp != NULL){
  160.           if(temp->data == value){
  161.               return TRUE;
  162.           }
  163.           temp = temp->next;
  164.       }
  165.       return FALSE;
  166.    }
  167. }
  168.  
  169. /**
  170.  * Set_Union: O(m+n) where m = Size(set1) and n = Size(set2)
  171.  *
  172.  *
  173.  *
  174. */
  175. void Set_Union(const Set* set1, const Set* set2, Set* ret){
  176.    if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
  177.        printf("One of Set Does not Exists are not initialized");
  178.        return;
  179.     }
  180.  
  181.    Set_Create(set1, ret);
  182.    const Set* temp = set2;
  183.  
  184.    while(temp != NULL){
  185.       SetEntry entry = temp->data;
  186.       Set_Insert(ret, entry);
  187.       temp = temp->next;
  188.    }
  189. }
  190.  
  191. void Set_Intersection(const Set* set1, const Set* set2, Set* ret){
  192.  
  193.    if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
  194.        printf("One of Set Does not Exists are not initialized");
  195.        return;
  196.     }
  197.  
  198.    Set_Create(ret);
  199.    const Set* temp = set1;
  200.  
  201.    while(temp != NULL){
  202.       SetEntry entry = temp->data;
  203.       if(Set_Contains(set2, entry) == TRUE){
  204.           Set_Insert(ret, entry);
  205.       }
  206.       temp = temp->next;
  207.    }
  208. }
  209.  
  210. double Set_Similarity(const Set* set1, const Set* set2){
  211.     if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
  212.        printf("One of Set Does not Exists are not initialized");
  213.        return -1.0;
  214.     }
  215.  
  216.     Set* set3;
  217.     Set_Union(set1, set2, set3);
  218.     Set* set4;
  219.     Set_Intersection(set1, set2, set4);
  220.     return (double) (Set_Size(set4)/Set_Size(set3));
  221. }
  222.  
  223. void Set_Print(const Set* set){
  224.  //   printf("Set Size = %d",set->size);
  225.    if(set == NULL || set->size == 0){
  226.       printf("Set Does not exists or does not contains any element");
  227.       return;
  228.    }
  229.  
  230.    printf("The Set Elements are: \n{");
  231.    while(set->next != NULL){
  232.        printf("%d, ",set->data);
  233.        set = set->next;
  234.    }
  235.    printf("%d}\n",set->data);
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement