Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
In this project, interfaces for mathematical set theory are implement using linked list in c programming language. End User gives the input like two list of data and,This program will give the outputs as the union and intersection of these two lists.
1.End user can two sets of the data as A=[1,2,3,4,5,6], and B[2,3,5,6,1].
2.After that program can give result as the union and intersection of these data A,B.

// Code
// (.h file)
#ifndef SET_H_
#define SET_H_
#include<stdio.h>
#include<stdlib.h>
//....Defining the Node....
typedef struct Node
{
int data;
struct Node *Next;
} Node; //User define data typedef declaration
// Static Variables Declaration
static Node *tempA,*tempB,*Set_A,*Set_B,*newnodeA,*newnodeB,*res_intersection,*res_union,*newnode,*head_intersection,*head_union;
//Functions Declaration
Node *insert_node(Node *head_ref,int n);//head ref data//
Node *get_union(Node *head_pointer1, Node *head_pointer2);
Node *get_intersection(Node *head_pointer1, Node *head_pointer2);
int compare(Node *head, int data);//compare the values of both the list//
#endif /* SET_H_ */
// Sub C file (.c file )
#include "set.h"
Node *insert_node(Node *head_ref,int n) // This function is created to insert elements to linked list .
{
newnode=(Node*)malloc(sizeof(Node)); //allocation of memory in heap
newnode->data = n;
newnode->Next=head_ref;
head_ref=newnode;
return head_ref;
}
int compare(Node *head, int data) // This function compare two different linked list
{
while(head != NULL)
{
if(head->data==data)
return 1;
head=head->Next;
}
return 0;
}
Node *get_union(Node *head_pointer1, Node *head_pointer2) // This function Creating Union
{
while(head_pointer1 != 0)//check whether the head pointer not equal to 0 or NULL//
{
head_union=insert_node(head_union,head_pointer1->data);
head_pointer1=head_pointer1->Next;
}
while(head_pointer2 != 0)
{
if (!compare(head_pointer2, head_pointer2->data))
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->Next;
}
return head_union;
}
Node *get_intersection(Node *head_pointer1,Node*head_pointer2)//This function Creating Intersection //
{
while(head_pointer1 != 0)//check whether the head pointer is not equal to 0 or NULL//
{
if (compare(head_pointer2, head_pointer1->data))
head_intersection=insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->Next;
}
return head_intersection;
}
// main C file (.c file)
#include "set.h"
int main()
{
int A,B,i; // local variables Declaration
// Allocation of memory in heap.
tempA = malloc(sizeof(Node));
tempB = malloc(sizeof(Node));
// Asking to user to enter the number of elements of 1st linked list.
printf("Enter the Number you are going to put in Set-A\n");
fflush(stdout);
scanf("%d",&A);
// Asking to user to enter items of 1st linked list.
printf("Enter Set-A elements:\n");
fflush(stdout);
scanf("%d",&tempA->data);
tempA->Next = NULL;
Set_A = tempA; // Creating 1st linked list
for(i = 1; i < A; i++)
{
newnodeA = malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(newnodeA->data));
newnodeA->Next = NULL;
Set_A->Next = newnodeA;
Set_A = Set_A->Next;
}
Set_A = tempA; // Printing elements of 1st linked list
printf("You entered Set-A as follows:\n");
while(Set_A != NULL)
{
printf("%d,",Set_A->data);
Set_A = Set_A->Next;
}
// Asking user to enter the number of elements of 2nd linked list
printf("\nEnter the Number you are going to put in Set-B\n");
fflush(stdout);
scanf("%d",&B);
// Asking to user to enter items of 2nd linked list.
printf("Enter Set-B elements:\n");
fflush(stdout);
scanf("%d",&tempB->data);
tempB->Next = NULL;
Set_B = tempB;
for(i = 1; i < B; i++) // Creating 2nd linked list
{
newnodeB = malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(newnodeB->data));
newnodeB->Next = NULL;
Set_B->Next = newnodeB;
Set_B = Set_B->Next;
}
Set_B = tempB; // Printing elements of 2nd linked list
printf("You entered Set-B as follows:\n");
while(Set_B != NULL)
{
printf("%d,",Set_B->data);
Set_B = Set_B->Next;
}
// Union of both linked list
Set_A = tempA;
Set_B = tempB;
res_union = NULL;
printf("\nUnion of Set-A and Set-B\n");
res_union = get_union(Set_A,Set_B); // calling get_union function
while(res_union != NULL)
{
printf("%d,",res_union->data);
res_union = res_union->Next;
}
// Intersection of both linked list
Set_A = tempA;
Set_B = tempB;
res_intersection = NULL;
printf("\nIntersection of Set-A and Set-B\n");
res_intersection = get_intersection(Set_A,Set_B); // calling get_intersection function
if(res_intersection == 0)//if there is no element common is both list then printing this.
{
printf("Set-A and Set-B have no common elements");
}
while(res_intersection != NULL)
{
printf("%d,",res_intersection->data);
res_intersection = res_intersection->Next;
}
return 0;
}