Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdbool.h> //Boolean Data Type
#include<stdio.h> //Preprocessor
#include<stdlib.h> //Memory Allocation
/* Linked List Node */
struct Node
{
int data;
struct Node* next;
}*head1,*head2,*uninLL,*intersecnLL;
void initialize() //Initialize for Two lists Starting at NULL
{
head1 = head2 = NULL;
}
/* Inserting a Node at the Beginning of a Linked List */
void insert(struct Node** head, int new_data);
/* Check if Element in Present in a Linked List */
bool Element_in_present(struct Node* head, int data);
struct Node* getunin(struct Node* head1,struct Node* head2) //Function to get union of 2 Linked List
{
struct Node* ans = NULL;
struct Node *t1 = head1, *t2 = head2;
//Insert all Elements of List1 to the answer List
while( t1 != NULL)
{
insert(&ans, t1->data);
t1 = t1->next;
}
//Insert Elements of List2 but Which are not present in answer list
while( t2 != NULL)
{
if(!Element_in_present(ans, t2->data))
insert(&ans, t2->data);
t2 = t2->next;
}
return ans;
}
struct Node* getintersection(struct Node* head1,struct Node* head2) //Function To Get intersection of 2 Linked List
{
struct Node* result = NULL;
struct Node* t1 = head1;
//Traverse List1 and Search Each of the Element of it in list2 if the Element is Present in list2,
//then Insert the Element to result
while( t1 != NULL)
{
if(Element_in_present(head2,t1->data))
insert(&result, t1->data);
t1 = t1->next;
}
return result;
}
/* insert a Node Beginning of a Linked List */
void insert(struct Node** head, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Memory Allocate Node
/* put in the data */
new_node->data = new_data;
/* link the Old list of the new_node */
new_node->next = *head;
/* Move the head to point to the new node */
*head = new_node;
}
/* Print The Linked List */
void printLinkedList (struct Node* node)
{
while (node != NULL)
{
printf("%d ",node->data);
node = node->next;
}
}
//When Boolean if Data is Present in Linked list else false
bool Element_in_present (struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)//True Integer Constant 1
return 1;
t = t->next;
}
return 0;
}
int main() // Main Class
{
int i,setA,setB,tmp; //Declared User Defined Variables
initialize(); //Function to Initialize Empty or NULL
printf("Enter Number of Nodes in First Linked List: ");
fflush(stdout); //Clear Buffer for Standard output
scanf("%d",&setA);
printf("Enter %d integer: \n",setA);
fflush(stdout);
for (i=0; i<setA; i++)
{
scanf("%d",&tmp);//Get values From Temporary
insert(&head1,tmp);//call Insert Function to insert values in tem to head1
}
printf("Enter Number of Nodes in Second Linked List: ");
fflush(stdout);
scanf("%d",&setB);
printf("Enter %d Data: \n",setB);
fflush(stdout);
for (i=0; i<setB;i++)
{
scanf("%d",&tmp);
insert(&head2,tmp);//call Insert Function to insert values in tem to head2
}
intersecnLL = getintersection(head1, head2);//Call Function Get union ans to assign
uninLL = getunin(head1, head2); //Call Function Get intersection Result to assign
printf("\nFirst List Of Data: ");
fflush(stdout);
printLinkedList(head1);//Call That Function Print that Values
printf("\nSecond List Of Data: ");
fflush(stdout);
printLinkedList(head2);//Call That Function Print that Values
printf("\n\nUnion Of Two List: ");
fflush(stdout);
printLinkedList(uninLL);//Function to Print Union Values
printf("\nIntersection Of Two List: ");
fflush(stdout);
printLinkedList(intersecnLL);//Function to Print Intersection Values
return 0;
}