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:
In this project we should write C code for Mathematics set theory. Here I used linked list to store the data and initialize the data. I creted c code for only Union and Intersction of two list
Goals :
1 - we should create a C code for mathematical set theory using linked list.
2 - We should create a c code for Union and Intersecction of mathematical set theory.
3 - We should create the code with understandable.
Key Highlights of the Project:
Creating two list of data using linked list method.
1 - I created 2 list using linked list.
2 - To store the data of two list, I was use for loop.
3 - To create linked list we should use structure.
4 - I used two element in structure. first is int data, second is struct node* next
one element used for store data and other one used for store the address of next node.
Insert the node to 1st and 2nd list accordingly.
1 - To insert the node in list I used strucrure.
2 - I used to insert the node for loop.
Intersection of data of two lists.
1- To intersection of two list I used two function.
2 - This Function will compare a list with another list
3 - Then the common numbers will be taken.
Getting Union of two list.
1- To intersection of two list I used two function.
2 - This Function will compare a list with another list
3 - Finally two list of numbers will be taken.
/*
* Project 1 Mathmatics set theory.c
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
int main()
{
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d", &n, &m);
// Start structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link list1
for(int i=0;i<n;i++)
{
printf("Enter the value in link list1 %d:-",i+1);
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp);
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("Enter the value in link list2 %d:-",i+1);
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp);
}
//call the function of intersection
intersecn = getIntersection(head1, head2);
//call the function of union
unin = getUnion(head1, head2);
printf("\n First list is ");
//function calling with structure pointer as argument
printList(head1);
printf("\n Second list is ");
printList(head2);
printf("\n Intersection list is ");
printList(intersecn);
printf("\n Union list is ");
printList(unin);
return 0;
}
/*
* fun.h
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#ifndef FUN_H_
#define FUN_H_
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data);//Function for assigning values
struct Node* getUnion(struct Node* head1, struct Node* head2);//Function for union set teory
struct Node* getIntersection(struct Node* head1, struct Node* head2);//Function for intersection set theory
void printList(struct Node* node);//Function for Print the input values
int isPresent(struct Node* head, int data);//Function for compare two list
#endif /* FUN_H_ */
/*
* getunion.c
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
struct Node* getUnion(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
// Insert all elements of list1 to the result list
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
{
push(&result, t2->data);
}
t2 = t2->next;
}
return result;
}
/*
* get intersection.c
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
// Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
{
push(&result, t1->data);
}
t1 = t1->next;
}
return result;
}
/*
*
* push.c
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// put in the data
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
/*
* is present.c
*
* Created on: 05-Feb-2023
* Author: Saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
int isPresent(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
/*
* print list.c
*
* Created on: 05-Feb-2023
* Author: saravanan TV
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include"fun.h"
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}