Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Solution: The goal of the project is to create a program that takes two lists of integers as input and computes their union and intersection. The union of two sets A and B is the set of all elements that are in A or B or both, while the intersection of two sets A and B is the set of all elements that are in both A and B.
Code Used For the Project
/*
* // This program reads in two integer arrays and computes their union and intersection.
* Project_01.c
*
* Created on: 23-Mar-2023
* Author: sagar
*/
#include <stdio.h>
#define MAX_SIZE 100 // Define the maximum size of the arrays
int main() {
// Declare the lists to be used
int list1[MAX_SIZE], list2[MAX_SIZE], union_list[MAX_SIZE], intersection[MAX_SIZE];
// Declare other variables to be used
int n1, n2, i, j, k, found, count;
// Read in list1
printf("Enter the number of elements in list1 (up to %d): ", MAX_SIZE);
fflush(stdout); // Flush the output buffer to ensure the message is printed before input is requested
scanf("%d", &n1);
printf("Enter the elements of list1 (separated by spaces): ");
fflush(stdout);
for (i = 0; i < n1; i++) {
scanf("%d", &list1[i]);
}
// Read in list2
printf("Enter the number of elements in list2 (up to %d): ", MAX_SIZE);
fflush(stdout);
scanf("%d", &n2);
printf("Enter the elements of list2 (separated by spaces): ");
fflush(stdout);
for (i = 0; i < n2; i++) {
scanf("%d", &list2[i]);
}
// Initialize union_list and intersection arrays to -1
for (i = 0; i < MAX_SIZE; i++) {
union_list[i] = -1; // Initialize all elements of union_list to -1
intersection[i] = -1; // Initialize all elements of intersection to -1
}
// Compute the union of list1 and list2
count = n1; // Set the count to the size of list1
for (i = 0; i < n2; i++) { // Iterate over each element of list2
found = 0; // Set found flag to 0
for (j = 0; j < n1; j++) { // Iterate over each element of list1
if (list2[i] == list1[j]) { // If the current element of list2 is equal to the current element of list1
found = 1; // Set found flag to 1
break; // Exit the loop
}
}
if (!found) { // If the current element of list2 was not found in list1
union_list[count] = list2[i]; // Add the current element of list2 to union_list
count++; // Increment the count
}
}
for (i = 0; i < n1; i++) { // Iterate over each element of list1
union_list[count] = list1[i]; // Add the current element of list1 to union_list
count++; // Increment the count
}
// Compute the intersection of list1 and list2
count = 0; // reset count for intersection
for (i = 0; i < n1; i++) {
for (j = 0; j < n2; j++) {
if (list1[i] == list2[j]) { // if the element is found in both lists, check if it's already in intersection
found = 0;
for (k = 0; k < count; k++) {
if (intersection[k] == list1[i]) { // if the element is already in intersection, set found flag and break
found = 1;
break;
}
}
if (!found) { // if the element is not found in intersection, add it and increment count
intersection[count] = list1[i];
count++;
}
break; // break from inner loop to avoid duplicate elements in intersection
}
}
}
// Print the results
printf("The union of the two lists is: ");
fflush(stdout);
for (i = 0; i < MAX_SIZE; i++) {
if (union_list[i] != -1) {
printf("%d ", union_list[i]);
fflush(stdout);
}
}
printf("\n");
fflush(stdout);
// Print the intersection of the two lists
printf("The intersection of the two lists is: ");
fflush(stdout);
for (i = 0; i < MAX_SIZE; i++) {
if (intersection[i] != -1) {
printf("%d ", intersection[i]);
fflush(stdout);
}
}
printf("\n");
fflush(stdout);
return 0;
}
The program works as follows:
-
Here user is prompted to enter the number of elements in list1 and list2, up to a maximum of MAX_SIZE (which is set to 100 in the code).
-
User is then prompted to enter the elements of list1 and list2, separated by spaces. The elements are read into two arrays, list1 and list2, respectively.
-
Two additional arrays, union_list and intersection, are initialized to hold the results of the computations.
-
The program then computes the union of list1 and list2. To do this, it first sets a count variable to the number of elements in list1. It then loops through each element in list2, checking if it already exists in list1. If it does not, it adds it to the end of the union_list array and increments the count variable to reflect the addition of a new element. Finally, the program adds any remaining elements from list1 to the union_list array.
-
The program then computes the intersection of list1 and list2. To do this, it loops through each element in list1 and checks if it exists in list2. If it does, the program checks if the element has already been added to the intersection array (to avoid adding duplicates). If it has not, it adds the element to the end of the intersection array and increments the count variable to reflect the addition of a new element.
-
Finally, the program prints the results of the computations to the console. The union_list and intersection arrays are printed, with any -1 values (which were used to initialize the arrays) ignored.
The logic for computing the union and intersection of two sets is a fundamental concept in mathematics, and can be applied in many different contexts. The program demonstrates how this concept can be implemented in a computer program, using basic loops and array manipulation.
Output For the program

The input for this program consists of two lists of integers: list1 with 4 elements {1, 2, 3, 4} and list2 with 6 elements {1, 2, 3, 4, 5, 6}.
The program prompts the user to enter the number of elements in list1 and list2, and then asks the user to enter the elements of each list separated by spaces.
After receiving the input, the program performs the union and intersection operations on the two lists using a for loop and if statements.
For the union operation, the program first copies all the elements of list1 to the result array. Then it checks each element of list2 against the elements already in the result array. If the element is not already in the result array, it adds it to the end of the result array.
For the intersection operation, the program checks each element of list1 against the elements of list2. If an element of list1 is also in list2 and is not already in the intersection array, it is added to the intersection array.
Finally, the program prints out the result of the union and intersection operations.
In this specific case, the union of list1 and list2 is {6, 5, 4, 1, 2, 3}, and the intersection is {1, 2, 3, 4}.