Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
PROJECT
AIM
To create user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
OBJECTIVE
-> 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.
STEPS INVOLVED
a. DEFINE NODE STRUCTURE
b. FUNCTIONS
1. INSERT A NEW ELEMENT AT THE END OF THE LINKED LIST
step1: Allocate memory.
Step2: Create a node and also initialize head pointer.
Step3: Check if the linked list is empty and if yes, set the new node as the head.
Step4: Make newnode as head.
Step5: Create another pointer temp and Traverse the linked list to the last node.
Step6: Store the new node at the end of the linked list.
2. PRINT THE ELEMENTS OF THE LINKED LIST
Display the elements stored in nodes after traversing.
3. FIND THE UNION OF TWO SETS
Step1: Create a new linked list to store the union result (res_union).
Step2: Traverse the first set and insert the first set in the union result.
Step3: Simillarly traverse the second set and insert its elements into the union result only if they are not already present in union result.
Step4: Display the union result.
4. FIND THE INTERSECTION OF TWO SETS
Step1: Create a new linked list to store the Intersection result (res_intersection).
Step2: Traverse the first set and insert the elements from first set in the Intersection result.
Step3: Simillarly traverse the second set and insert its elements into the union result only if they are present in Intersection result.
Step4: Display the Intersection result.
c. MAIN FUNCTION
Step1:Create first and second set by getting inputs from user.
Step2: Enter the elements respectively.
Step3: Print its respective elements.
Step4:Display union result.
Step5:Display Intersection result.
PROGRAM CODE
/*
* Settheorylinkedlist.c
* C Program to implement Mathematical Set theory using Linked List
* Created on: 16-Feb-2023
* Author: dinesh
*/
#include <stdio.h> //Preprocessors
#include <stdlib.h>
//1. DEFINE NODE STRUCTURE
struct node
{
int data;
struct node *next;
};
//2. INSERT A NEW ELEMENT AT THE END OF THE LINKED LIST
void insertElement(struct node** head, int data)
{
//Create a new node.
struct node* newNode = (struct node*) malloc(sizeof(struct node));//Allocate memory
newNode->data = data;
newNode->next = NULL;
//To check the linked list is empty.
//If yes, set the new node as the head
if (*head == NULL)
{
*head = newNode;
return;
}
//Traverse the linked list to the last node.
struct node* temp = *head;
while (temp->next!= NULL)
{
temp = temp->next;
}
temp->next = newNode; //Insert the new node at the end of the linked list
}
//3. PRINT THE ELEMENTS OF THE LINKED LIST
void display_list(struct node* head)
{
struct node* temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
//4. FIND THE UNION OF TWO SETS
struct node* get_union(struct node* set1, struct node* set2)
{
//Create a new linked list to store the union result.
struct node* res_union = NULL;
//Traverse the first set.
struct node* temp = set1;
while (temp != NULL)
{
insertElement(&res_union, temp->data);//Insert elements in union result
temp = temp->next;
}
//Traverse the second set.
temp = set2;
while (temp != NULL)
{
//Check if the element is present in the second set.
struct node* check = res_union;
while (check != NULL && check->data != temp->data)
{
check = check->next;
}
//To check if the element is already present in the union result.
//If the element is not present, insert it into the union result.
if (check == NULL)
{
insertElement(&res_union, temp->data);
}
temp = temp->next;
}
return res_union;
}
//5. FIND THE INTERSECTION OF TWO SETS
struct node* get_intersection(struct node* set1, struct node* set2)
{
//Create a new linked list to store the intersection result.
struct node* res_intersection = NULL;
struct node* temp = set1;
while (temp != NULL) //Traverse the first set.
{
//Check if the element is present in the second set.
struct node* check = set2;
while (check != NULL && check->data != temp->data)
{
check = check->next;
}
//To check if each element is present in the second set.
//If the element is present, insert it into the intersection result.
if (check != NULL)
{
insertElement(&res_intersection, temp->data);
}
temp = temp->next;
}
return res_intersection;
}
//MAIN FUNCTION
int main()
{
int n1, n2, data; //variable declaration.
// Create the first set.
struct node* set1 = NULL;
printf("Enter the number of elements in first set: ");
fflush(stdout);
scanf("%d", &n1);
// Ask user to enter the elements of the first set.
printf("Enter the elements of first set:\n");
fflush(stdout);
for (int i = 0; i < n1; i++)
{
scanf("%d", &data);
insertElement(&set1, data);
}
// Create the second set.
struct node* set2 = NULL;
printf("Enter the number of elements in the second set: ");
fflush(stdout);
scanf("%d", &n2);
// Ask the user to enter the elements of the second set.
printf("Enter the elements of second set:\n");
fflush(stdout);
for (int i = 0; i < n2; i++)
{
scanf("%d", &data);
insertElement(&set2, data);
}
// Print the elements of the first set.
printf("First set elements: ");
display_list(set1);
// Print the elements of the second set.
printf("Second Set elements: ");
display_list(set2);
// Find the union of the two sets.
struct node* res_union = get_union(set1, set2);
printf("\nUnion: ");
display_list(res_union);
// Find the intersection of the two sets.
struct node* res_intersection = get_intersection(set1, set2);
if (res_intersection!=NULL)
{
printf("\nIntersection: ");
display_list(res_intersection);
}
else
{
printf("\nIntersection: There are no common elements");
}
return(0);
}
OUTPUT
