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
The project focuses on implementing a set theory inteface using c programming language. User can input to data lists and the program computes the outputs, union and intersection of the elements of two lists.
Goals
- Implemet a set theory interface in C
- Accept user input for two lists and provide the union and intersection as output
Key Higlights of the Project
- Create link list using DMA
- user friendly input mechanism for specifying list element
- Efficient computation of intersection and union sets
- Proper memory management to prevent memory leaks
Deliverables of this Project
- Program that computes and display the intersection of two lists
- Program that computes and disply th union of two lists
Expected outcome
- Understanding of linked list implementation in C
- Proficiency in handling dynamic memory allocation
- Practical experience in set theory operations like union and intersection
- Appreciation of proper memory management practices in C programming
Program
#include
#include
struct Node {
int data;
struct Node* next;
};
void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
struct Node* intersection(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
while (list1 != NULL && list2 != NULL) {
if (list1->data == list2->data) {
insertNode(&result, list1->data);
list1 = list1->next;
list2 = list2->next;
} else if (list1->data < list2>data) {
list1 = list1->next;
} else {
list2 = list2->next;
}
}
return result;
}
struct Node* unionSet(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
while (list1 != NULL) {
insertNode(&result, list1->data);
list1 = list1->next;
}
while (list2 != NULL) {
insertNode(&result, list2->data);
list2 = list2->next;
}
return result;
}
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
int n1, n2;
// Input for List 1
printf("Enter the number of elements in List 1: ");
scanf("%d", &n1);
printf("Enter �lements for List 1: ", n1);
for (int i = 0; i < n1>
int element;
scanf("%d", &element);
insertNode(&list1, element);
}
// Input for List 2
printf("Enter the number of elements in List 2: ");
scanf("%d", &n2);
printf("Enter �lements for List 2: ", n2);
for (int i = 0; i < n2>
int element;
scanf("%d", &element);
insertNode(&list2, element);
}
// Print the original lists
printf("\nList 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
// Compute and print the intersection
printf("\nIntersection: ");
struct Node* intersectionResult = intersection(list1, list2);
printList(intersectionResult);
// Compute and print the union
printf("Union: ");
struct Node* unionResult = unionSet(list1, list2);
printList(unionResult);
// Free allocated memory
freeList(list1);
freeList(list2);
freeList(intersectionResult);
freeList(unionResult);
return 0;
}
Expecting Output form the above program
[?2004l
Enter the number of elements in List 1: 4
Enter 4 elements for List 1: 2 4 6 8
Enter the number of elements in List 2: 3
Enter 3 elements for List 2: 4 8 10
List 1: 2 4 6 8
List 2: 4 8 10
Intersection: 4 8
Union: 2 4 6 8 4 8 10
Screenshot of program execution

Result
Interface for mathematical set theory using C programming lanuage was implemented,executed and tested.