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>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *LL1, *LL2, *unionLL, *intersectionLL;
void initialize()
{
LL1=LL2=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* findunion(struct node *LL1, struct node *LL2)
{
unionLL= NULL;
struct node *temp = LL1;
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->next;
}
while(LL2!=NULL)
{
if(!search(LL1, LL2->data))
{
insert(&unionLL, LL2->data);
}
LL2 = LL2->next;
}
return unionLL;
}
struct node* intersection(struct node *LL1, struct node *LL2)
{
intersectionLL = NULL;
while(LL1!=NULL)
{
if(search(LL2, LL1->data))
{
insert(&intersectionLL, LL1->data);
}
LL1=LL1->next;
}
return intersectionLL;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr!=NULL)
{
printf("%d --> ", nodePtr->data);
nodePtr = nodePtr->next;
}
}
int main()
{
int i, LL1Count, LL2Count, temp;
initialize();
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LL1Count);
printf("Enter %d integers\n", LL1Count);
for(i=0; i<LL1Count; i++)
{
scanf("%d", &temp);
insert(&LL1, temp);
}
printLinkedList(LL1);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LL2Count);
printf("Enter %d integers\n", LL2Count);
for(i=0; i<LL2Count; i++)
{
scanf("%d", &temp);
insert(&LL2, temp);
}
printLinkedList(LL2);
findunion(LL1, LL2);
intersection(LL1, LL2);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);
return 0;
}