Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
// A STRUCTURE OF LINKED LIST NODE
struct node
{
int data;
struct node *next;
} *LLA, *LLB, *LLunion, *LLintersection;
void initialize()
{
LLA = LLB = NULL;
}
void insert(struct node **head, int num) //GIVEN A INSERTS A NODE IN FRONT OF A SINGLY LINKED LIST
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); //CREATE A NEW LINKED LIST NODE
newNode->data = num;
newNode->next = *head; //NEXT POINTER OF NEW NODE WILL POINT TO HEAD NODE OF LINKED LIST
*head = newNode; //MAKE NEW NODE AS NEW HEAD OF LINKED LIST
}
int search(struct node *head, int num) //SEARCHE AN ELEMENT IN LINKED LIST BY TRANSVERSING FROM HEAD TO TAIL
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* findunion(struct node *LLA, struct node *LLB) //RETURN THE UNION OF TWO GIVEN LINKED LIST
{
LLunion = NULL;
struct node *temp = LLA; //ADD ALL NODES IF FIRST LINKED LIST TO LLUNION
while(temp != NULL)
{
insert(&LLunion, temp->data);
temp = temp->next;
}
while(LLB != NULL)
{
if(!search(LLA, LLB->data))
{
insert(&LLunion, LLB->data);
}
LLB = LLB->next;
}
return LLunion;
}
struct node* intersection(struct node *LLA, struct node *LLB) //RETURN THE LINKED LIST WHICH CONTAIN COMMON NODES OF TWO GIVEN LINKED LIST
{
LLintersection = NULL;
while(LLA != NULL) //SEARCH EVERY ELEMENT OF LLA, IF FOUND THEN ADD TO THE INTERSECTION LIST
{
if(search(LLB, LLA->data))
{
insert(&LLintersection, LLA->data);
}
LLA = LLA->next;
}
return LLintersection;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{ { { return 0;
int i, LLACount, LLBCount, temp;
initialize();
//CREATING FIRST LINKED LIST
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLACount);
printf("Enter %d integers\n", LLACount);
for(i=0; i
scanf("%d", &temp);
insert(&LLA, temp);
}
printLinkedList(LLA);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLBCount);
printf("Enter %d integers\n", LLBCount);
for(i=0; i
scanf("%d", &temp);
insert(&LLB, temp);
}
printLinkedList(LLB);
findunion(LLA, LLB);
intersection(LLA, LLB);
printf("\nUnion Linked List\n");
printLinkedList(LLunion); //PRINTING UNION LINKED LIST
printf("\nIntersection Linked List\n");
printLinkedList(LLintersection); //PRINTING INTERSECTION LINKED LIST
}