Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
//* Project 1: Program of mathemathical set theory(find union and intersection of two nodes using linklist *//
#include<stdio.h>
#include<stdlib.h>
// A structure of linked list node
struct node
{
int data;
struct node *next;
} *LLamol, *LLraj, *unionLL, *intersectionLL;
void initialize()
{
LLamol = LLraj = NULL;
}
//Given a Inserts a node in front of a singly linked list//
void insert(struct node **head, int num)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}
// Searches an element in Linked List by linearly traversing from head to tail
int compare(struct node *head, int num)
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
// Returns the union of two given linked list
struct node* findunion(struct node *LLamol, struct node *LLraj)
{
unionLL = NULL;
// Add all nodes of first Linked List to unionLL
struct node *temp = LLamol;
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(LLraj != NULL)
{
if(!compare(LLamol, LLraj->data))
{
insert(&unionLL, LLraj->data);
}
LLraj = LLraj->next;
}
return unionLL;
}
//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *LLamol, struct node *LLraj)
{
intersectionLL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LLamol != NULL)
{
if(compare(LLraj, LLamol->data))
{
insert(&intersectionLL, LLamol->data);
}
LLamol = LLamol->next;
}
return intersectionLL;
}
// Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, LLamolCount, LLrajCount, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLamolCount);
printf("Enter %d integers\n", LLamolCount);
for(i=0; i<LLrajCount; i++)
{
scanf("%d", &temp);
insert(&LLamol, temp);
}
printLinkedList(LLamol);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLrajCount);
printf("Enter %d integers\n", LLrajCount);
for(i=0; i<LLrajCount; i++)
{
scanf("%d", &temp);
insert(&LLraj, temp);
}
printLinkedList(LLraj);
// printing Union of two given linked list
findunion(LLamol, LLraj);
intersection(LLamol, LLraj);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);
return 0;
}