Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <malloc.h>
- # include "set.h"
- /**
- * Set_Create: O(1)
- * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
- * Set.
- */
- void Set_Create(Set* set){
- if(set == NULL){
- set = (Set *)malloc(sizeof(Set));
- }
- if(set == NULL)
- printf("Error");
- // printf("Hello\n");
- set->size = 0;
- // printf("Size = %d",set->size);
- set->data = -1;
- set->next = NULL;
- }
- /**
- * Set_Create: 0(n) where n = Size(set1)
- *
- * Function Used to create set2 such that it is initialized with the same values as set1
- * This function is traverses set1 completely to create set2. Hence Linear Time Complexity
- */
- void Set_Create(const Set* set1, Set* set2){
- Set_Create(set2);
- const Set* temp = set1;
- while(temp != NULL){
- set2->data = temp->data;
- temp = temp->next;
- set2->next = (Set *)malloc(sizeof(Set));
- set2 = set2->next;
- }
- set2->next = NULL;
- }
- /**
- * Set_Clear: O(1)
- * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
- * Set.
- */
- void Set_Clear(Set* set){
- Set_Create(set);
- }
- /**
- * Set_Size: O(1)
- * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
- * Set.
- */
- int Set_Size(const Set* set) {
- if(set == NULL){
- return -1;
- }
- return set->size;
- }
- /**
- * Set_Empty: O(1)
- * This function has constant time complexity because it performs number of operations which are independent of number of elements present in the
- * Set.
- */
- Boolean Set_Empty(const Set* set){
- if(set == NULL || set->size == 0){
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Set_Insert: O(n)
- * This function has constant time complexity because it Traversese each element of set before inserting to make sure that no duplicate values are
- * present in the set.
- */
- Boolean Set_Insert(Set* set, SetEntry value){
- if(set == NULL){
- printf("Set is not initialized");
- return FALSE;
- }
- if(set->size == 0){
- set->data = value;
- ++set->size;
- return TRUE;
- }
- else {
- Set* temp = set;
- while(temp->next != NULL){
- if(temp->data == value) //.. Can not Insert Value as it is already present in the Set
- return FALSE;
- temp = temp->next;
- }
- temp->next = (Set *)malloc(sizeof(Set));
- temp = temp->next;
- temp->data = value;
- temp->next = NULL;
- ++(set->size);
- return TRUE;
- }
- }
- /**
- * Set_Delete: O(n)
- *
- * This Function Traverses set entirely (in worst case) in sequential manner to find the desired value. Hence Linear Time Complexity
- */
- Boolean Set_Delete(Set* set, SetEntry value){
- if(set == NULL || set->size == 0){
- printf("Set Does not Exists or not initialized");
- return FALSE;
- }
- else{
- Set* temp = set;
- Set* tempprev = set;
- while(temp != NULL){
- if(temp->data == value){
- if(temp == set){ // if it is the first element.
- tempprev = set;
- free(set);
- set = tempprev->next;
- }
- else {
- tempprev->next = temp->next;
- }
- --(temp->size);
- return TRUE;
- }
- tempprev = temp;
- temp = temp->next;
- }
- printf("Set Entry to be removed not Found");
- return FALSE;
- }
- }
- /**
- * Set_Contains: O(n)
- *
- * Linear Time Complexity because to find the match, entire set has to be traversed in a sequential manner (in worst case).
- *
- */
- Boolean Set_Contains(const Set* set, SetEntry value){
- if(set == NULL || set->size == 0){
- printf("Set Does not Exists ");
- return FALSE;
- }
- else {
- const Set* temp = set;
- while(temp != NULL){
- if(temp->data == value){
- return TRUE;
- }
- temp = temp->next;
- }
- return FALSE;
- }
- }
- /**
- * Set_Union: O(m+n) where m = Size(set1) and n = Size(set2)
- *
- *
- *
- */
- void Set_Union(const Set* set1, const Set* set2, Set* ret){
- if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
- printf("One of Set Does not Exists are not initialized");
- return;
- }
- Set_Create(set1, ret);
- const Set* temp = set2;
- while(temp != NULL){
- SetEntry entry = temp->data;
- Set_Insert(ret, entry);
- temp = temp->next;
- }
- }
- void Set_Intersection(const Set* set1, const Set* set2, Set* ret){
- if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
- printf("One of Set Does not Exists are not initialized");
- return;
- }
- Set_Create(ret);
- const Set* temp = set1;
- while(temp != NULL){
- SetEntry entry = temp->data;
- if(Set_Contains(set2, entry) == TRUE){
- Set_Insert(ret, entry);
- }
- temp = temp->next;
- }
- }
- double Set_Similarity(const Set* set1, const Set* set2){
- if(set1 == NULL || set1->size == 0 || set2 == NULL || set2->size == 0){
- printf("One of Set Does not Exists are not initialized");
- return -1.0;
- }
- Set* set3;
- Set_Union(set1, set2, set3);
- Set* set4;
- Set_Intersection(set1, set2, set4);
- return (double) (Set_Size(set4)/Set_Size(set3));
- }
- void Set_Print(const Set* set){
- // printf("Set Size = %d",set->size);
- if(set == NULL || set->size == 0){
- printf("Set Does not exists or does not contains any element");
- return;
- }
- printf("The Set Elements are: \n{");
- while(set->next != NULL){
- printf("%d, ",set->data);
- set = set->next;
- }
- printf("%d}\n",set->data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement