Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
// Q1 :
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *L1, *L2, *UL, *IL;
void initialize()
{
L1 = L2 = NULL;
}
void insert(struct node **head, int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = *head;
*head = newNode;
}
int search(struct node *head, int num)
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* find_union(struct node *LLOne, struct node *LLTwo)
{
UL = NULL;
struct node *temp = LLOne;
while(temp != NULL)
{
insert(&UL, temp->data);
temp = temp->next;
}
while(L2 != NULL)
{
if(!search(LLOne, LLTwo->data))
{
insert(&UL, LLTwo->data);
}
LLTwo = LLTwo->next;
}
return UL;
}
struct node* intersection(struct node *LLOne, struct node *LLTwo)
{
IL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LLOne != NULL)
{
if(search(LLTwo, LLOne->data))
{
insert(&IL, LLOne->data);
}
LLOne = LLOne->next;
}
return IL;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, L1_Count, L2_Count, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &L1_Count);
printf("Enter %d integers\n", L1_Count);
for(i=0; i<L1_Count; i++)
{
scanf("%d", &temp);
insert(&L1, temp);
}
printLinkedList(L1);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &L2_Count);
printf("Enter %d integers\n", L2_Count);
for(i=0; i<L2_Count; i++)
{
scanf("%d", &temp);
insert(&L2, temp);
}
printLinkedList(L2);
//printing Union of two given linked list
find_union(L2, L2);
intersection(L1, L1);
printf("\nUnion Linked List\n");
printLinkedList(UL);
printf("\nIntersection Linked List\n");
printLinkedList(IL);
return 0;
}