Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
_______________________________Program:___________________________
#include <stdio.h>
#include <stdlib.h>
// Definition of a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the list
void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to print the list
void printList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
return;
}
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to check if an element is present in the list
int isPresent(struct Node* head, int data) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == data) {
return 1; // Element found
}
temp = temp->next;
}
return 0; // Element not found
}
// Function to find the union of two sets
struct Node* unionSets(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
struct Node* temp = list1;
// Insert all elements from the first list
while (temp != NULL) {
insertNode(&result, temp->data);
temp = temp->next;
}
// Insert elements from the second list, only if they are not already in the result
temp = list2;
while (temp != NULL) {
if (!isPresent(result, temp->data)) {
insertNode(&result, temp->data);
}
temp = temp->next;
}
return result;
}
// Function to find the intersection of two sets
struct Node* intersectionSets(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
struct Node* temp = list1;
// Insert only the elements that are present in both lists
while (temp != NULL) {
if (isPresent(list2, temp->data) && !isPresent(result, temp->data)) {
insertNode(&result, temp->data);
}
temp = temp->next;
}
return result;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* unionList = NULL;
struct Node* intersectionList = NULL;
int n, value;
// Input for first list
printf("Enter the number of elements for the first list: ");
scanf("%d", &n);
printf("Enter elements for the first list:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &value);
insertNode(&list1, value);
}
// Input for second list
printf("Enter the number of elements for the second list: ");
scanf("%d", &n);
printf("Enter elements for the second list:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &value);
insertNode(&list2, value);
}
// Display the input lists
printf("\nFirst List: ");
printList(list1);
printf("Second List: ");
printList(list2);
// Find the union and intersection of the two lists
unionList = unionSets(list1, list2);
intersectionList = intersectionSets(list1, list2);
// Display the union and intersection results
printf("\nUnion of the two lists: ");
printList(unionList);
printf("Intersection of the two lists: ");
printList(intersectionList);
return 0;
}
OUTPUT:
Enter the number of elements for the first list: 5
Enter elements for the first list:
1 2 5 7 8
Enter the number of elements for the second list: 6
Enter elements for the second list:
8 9 11 52 96 54
First List: 1 2 5 7 8
Second List: 8 9 11 52 96 54
Union of the two lists: 1 2 5 7 8 9 11 52 96 54
Intersection of the two lists: 8