Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stdio.h> // Header file
#include <stdlib.h>
// A structure for linked list nodes LinkedList_1,LinkedList_2, unionLL, intersectionLL
struct node
{
int data;
struct node *next;
} *LinkedList_1, *LinkedList_2, *unionLL, *intersectionLL;
void initialize() // Intializing nodes
{
LinkedList_1 = LinkedList_2 = NULL;
}
//Given a Inserts a node in front of a singly linked list.
void insert(struct node **head, int num) //Insertion function
{
// 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 search(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 *LinkedList_1, struct node *LinkedList_2)
{
unionLL = NULL;
// Add all nodes of first Linked List to unionLL
struct node *temp = LinkedList_1;
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(LinkedList_2 != NULL)
{
if(!search(LinkedList_1, LinkedList_2->data))
{
insert(&unionLL, LinkedList_2->data);
}
LinkedList_2 = LinkedList_2->next;
}
return unionLL;
}
//Intersetion gives the common nodes present in linked list
struct node* intersection(struct node *LinkedList_1, struct node *LinkedList_2)
{
intersectionLL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LinkedList_1 != NULL)
{
if(search(LinkedList_2, LinkedList_1->data))
{
insert(&intersectionLL, LinkedList_1->data);
}
LinkedList_1 = LinkedList_1->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, LLOneCount, LLTwoCount, temp;
initialize();
//Creating First linked List
printf("\nEnter number of nodes in first Linked List\n");
scanf("%d", &LLOneCount);
printf("Enter %d integers\n", LLTwoCount);
for(i=0; i<LLOneCount; i++)
{
scanf("%d", &temp);
insert(&LinkedList_1, temp);
}
printLinkedList(LinkedList_1);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLTwoCount);
printf("Enter %d integers\n", LLTwoCount);
for(i=0; i<LLTwoCount; i++)
{
scanf("%d", &temp);
insert(&LinkedList_2, temp);
}
printLinkedList(LinkedList_2);
//printing Union of two given linked list
findunion(LinkedList_1, LinkedList_2);
intersection(LinkedList_1, LinkedList_2);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);
return 0;
}