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*/
#include <stdio.h>
#include <stdlib.h>//to use the malloc function
struct node//creating a structure of linked list node
{
int data;//for data part of node
struct node *link;//for link part of the node
} *LLOne, *LLTwo, *unionLL, *intersectionLL;
void initialize()
{
LLOne = LLTwo = NULL;
}
//adding node at starting of the linked list
void insert(struct node **head, int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));//creating new linked list
node
newNode->data = num;
newNode->link = *head;//link part of the new node is updated with adress of already existing first node
*head = newNode;//head pointer must need to point towards first node
}
/*visting each node from head to end node*/
int search(struct node *head, int num)
{
while (head != NULL)//this loop run until head equal
{
if (head->data == num)//data part of the first node
{
return 1;
}
head = head->link;//now head will point to the next node of list
}
return 0;
}
//union of two linked list
struct node* findunion(struct node *LLOne, struct node *LLTwo)
{
unionLL = NULL;
//adding linkedlist one to the union
struct node *temp = LLOne;
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->link;//now temp will points to the next node of the list
}
//inserting the nodes of linked list two which are absent in one
while(LLTwo != NULL)
{
if(!search(LLOne, LLTwo->data))
{
insert(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->link;//now ll2 points to the next node of the list
}
return unionLL;
}
//intersection nodes
struct node* intersection(struct node *LLOne, struct node *LLTwo)
{
intersectionLL = NULL;
//finding ll1 in ll2
while(LLOne != NULL)
{
if(search(LLTwo, LLOne->data))
{
insert(&intersectionLL, LLOne->data);
}
LLOne = LLOne->link;//now ll1 points to the next node of the list
}
return intersectionLL;
}
//printing linked list
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->link;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, LLOneCount, LLTwoCount, temp;
initialize();
//creating linked list one
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLOneCount);
printf("Enter %d node data\n", LLOneCount);
for(i=0; i<LLOneCount; i++)
{
scanf("%d", &temp);
insert(&LLOne, temp);
}
printLinkedList(LLOne);//printing linked list one by using this function
//creating linked list two
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLTwoCount);
printf("Enter %d node data\n", LLTwoCount);
for(i=0; i<LLTwoCount; i++)
{
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo);//printing linked list two by using this function
findunion(LLOne, LLTwo);/*finding union of the two linked list by
using the the findunion function which is already contains few lines of code in that function*/
intersection(LLOne, LLTwo);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);//printing the intersection nodes
return 0;
}
