Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview of the Project: (5 to 6 lines)
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists
Goals (2 to 3 points)
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key Highlights of the Project: (4 to 5 point)
- Creating two list of data using linked list method.
- Insert the node to 1st and 2nd list accordingly.
- Intersection of data of two lists.
- Getting Union of two list.
Deliverables of this project: (4 to 5 points)
- Intersection of data of two list.
- Union of data of two list
Expected Outcomes: (4 to 5 points)
- Student will get the Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.
Code
#include <stdio.h>
#include <stdlib.h>
/* define a set data structure */
typedef struct set {
int* elements;
int size;
} set;
/* function to create a new empty set */
set* create_set() {
set* new_set = malloc(sizeof(set));
new_set->elements = NULL;
new_set->size = 0;
return new_set;
}
/* function to add an element to a set */
void add_element(set* s, int element) {
int i;
/* check if element already exists in the set */
for (i = 0; i < s->size; i++) {
if (s->elements[i] == element) {
return;
}
}
/* resize the elements array */
s->elements = realloc(s->elements, (s->size + 1) * sizeof(int));
s->elements[s->size] = element;
s->size++;
}
/* function to compute the union of two sets */
set* union_set(set* s1, set* s2) {
set* union_set = create_set();
int i;
/* add elements from the first set */
for (i = 0; i < s1->size; i++) {
add_element(union_set, s1->elements[i]);
}
/* add elements from the second set */
for (i = 0; i < s2->size; i++) {
add_element(union_set, s2->elements[i]);
}
return union_set;
}
/* function to compute the intersection of two sets */
set* intersection_set(set* s1, set* s2) {
set* intersection_set = create_set();
int i, j;
/* check for elements that exist in both sets */
for (i = 0; i < s1->size; i++) {
for (j = 0; j < s2->size; j++) {
if (s1->elements[i] == s2->elements[j]) {
add_element(intersection_set, s1->elements[i]);
break;
}
}
}
return intersection_set;
}
/* function to print the elements of a set */
void print_set(set* s) {
int i;
printf("{ ");
for (i = 0; i < s->size; i++) {
printf("%d ", s->elements[i]);
}
printf("}\n");
}
/* main function */
int main() {
set* s1 = create_set();
set* s2 = create_set();
int n1, n2, i, element;
/* get input for first set */
printf("Enter the size of the first set: ");
scanf("%d", &n1);
printf("Enter the elements of the first set: ");
for (i = 0; i < n1; i++) {
scanf("%d", &element);
add_element(s1, element);
}
/* get input for second set */
printf("Enter the size of the second set: ");
scanf("%d", &n2);
printf("Enter the elements of the second set: ");
for (i = 0; i < n2; i++) {
scanf("%d", &element);
add_element(s2, element);
}
/* compute the union and intersection of the sets */
set* union_s = union_set(s1, s2);
set* intersection_s = intersection_set(s1, s2);
/* print the results */
printf("\nUnion of the sets: ");
print_set(union_s);
printf("Intersection of the sets: ");
print_set(intersection_s);
/* free memory */
free(s1->elements);
free(s1);
free(s2->elements);
free(s2);
free(union_s->elements);
free(union_s);
free(intersection_s->elements);
free(intersection_s);
return 0;
}