Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Project Creation of user defined data type to implement the Mathematical Set Theory in C
Aim:
The aim of the project is to implement the mathematical set theory using the C programming language by creating user-defined data types. This involves designing and utilizing linked lists to represent sets, implementing operations such as union and intersection, and providing a user interface for input and output of sets.
Here is the code:
/*
* union.c
*
* Created on: Nov 21, 2023
* Author: laxma
*/
#include<stdio.h>
#include<stdlib.h>
// node structure for linked list
struct Node {
int data; // Data of the node
struct Node* next; // Pointer to the next node in the linked list
};
// function to insert a node at the end of the linked list
void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for a new node
newNode->data = data; // Set data for the new node
newNode->next = NULL;
if (*head == NULL) {
*head = newNode; // If the list is empty, set the new node as the head
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next; // Traverse the list to find the last node
}
temp->next = newNode; // Set the next of the last node to the new node
}
}
//function to print elements of a linked list
void printlist(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d", temp->data); // Print the data of the current node
temp = temp->next; // Move to the next node
}
printf("\n");
}
// function to find the intersection of two linked lists
struct Node* findIntersection(struct Node* list1, struct Node* list2) {
struct Node* result = NULL; // Resultant linked list for intersection
struct Node** lastptr = &result; // Pointer to the last node in the result list
while (list1 != NULL && list2 != NULL) {
if (list1->data == list2->data) {
insertNode(lastptr, list1->data); // Insert common element into the result list
lastptr = &((*lastptr)->next); // Move the pointer to the last node
list1 = list1->next; // Move to the next node in list1
list2 = list2->next; // Move to the next node in list2
} else if (list1->data < list2->data) {
list1 = list1->next; // Move to the next node in list1
} else {
list2 = list2->next; // Move to the next node in list2
}
}
return result;
}
//function to find the union of two linked lists
struct Node* findUnion(struct Node* list1, struct Node* list2) {
struct Node* result = NULL; // Resultant linked list for union
struct Node** lastptr = &result; // Pointer to the last node in the result list
while (list1 != NULL || list2 != NULL) {
if (list1 != NULL && list2 != NULL && list1->data == list2->data) {
insertNode(lastptr, list1->data); // Insert common element into the result list
lastptr = &((*lastptr)->next); // Move the pointer to the last node
list1 = list1->next; // Move to the next node in list1
list2 = list2->next; // Move to the next node in list2
} else if (list1 == NULL || (list2 != NULL && list1->data < list2->data)) {
insertNode(lastptr, list2->data); // Insert unique element from list2 into the result list
lastptr = &((*lastptr)->next); // Move the pointer to the last node
list2 = list2->next; // Move to the next node in list2
} else {
insertNode(lastptr, list1->data); // Insert unique element from list1 into the result list
lastptr = &((*lastptr)->next); // Move the pointer to the last node
list1 = list1->next; // Move to the next node in list1
}
}
return result;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
int n, data;
//Inputs for the first lists
printf("Enter the number of elements for the list1: ");
fflush(stdout); // Flush the output buffer to ensure prompt is displayed
scanf("%d", &n);
printf("Enter the elements for the list1: ");
fflush(stdout); // Flush the output buffer to ensure prompt is displayed
for(int i=0; i<n; ++i) {
scanf("%d", &data);
insertNode(&list1, data); // Insert elements into the first list
}
//inputs for the second list
printf("Enter the number of elements for the list2: ");
fflush(stdout); // Flush the output buffer to ensure prompt is displayed
scanf("%d", &n);
printf("Enter the elements for the list2: ");
fflush(stdout); // Flush the output buffer to ensure prompt is displayed
for(int i=0; i<n; ++i) {
scanf("%d", &data);
insertNode(&list2, data); // Insert elements into the second list
}
//print the original lists
printf("List1: ");
printlist(list1);
fflush(stdout);
printf("List2: ");
printlist(list2);
//find the print intersection
struct Node* intersection = findIntersection(list1, list2);
printf("Intersection: ");
printlist(intersection);
//find and print union
struct Node* unionList = findUnion(list1, list2);
printf("Union: ");
printlist(unionList);
return 0;
}
Output:
Enter the number of elements for the list1: 5
Enter the elements for the list1: 1 3 5 7 9
Enter the number of elements for the list2: 5
Enter the elements for the list2: 1 2 5 4 9
List1: 13579
List2: 12549
Intersection: 159
Union: 135792549
Result:
This project is a C program that allows users to work with mathematical sets. Users can input two lists of data, and the program provides the output for the union and intersection of these two sets. The program utilizes a linked list data structure to represent sets, and it includes functions for inserting nodes, finding the intersection of sets, and finding the union of sets. The project also aims to provide a practical understanding of creating nodes using linked lists, comparing elements of two lists, and performing set operations in the context of mathematical set theory.