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 for standard input and outpurt
#include<stdlib.h> //headrerr file for standard library declaration
struct node // syntax for linkedlist initialization
{
int data; // variable declaration
struct node *next; //pointer for holding the adress
} *LLOne, *LLTwo, *unionLL, *intersectionLL; // refering to node syntax , made 4 no of linkedllist
void initialize()
{
LLOne = LLTwo = NULL; //initializing first and second linkedlist will be NULL
}
void insert(struct node **head, int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); //assigning the memory for node
newNode->data = num; // newnode is storing data from num
newNode->next = *head; //new node hold the adress
*head = newNode; //head is holding new node
}
int search(struct node *head, int num) //traversal of head value
{
while (head != NULL) {
if (head->data == num){
return 1;
}
head = head->next;
}
return 0;
}
struct node* findunion(struct node *LLOne, struct node *LLTwo) //logic for union
{
unionLL = NULL; //initializing union as NULL
struct node *temp = LLOne; //temp variable holding
while(temp != NULL)
{
insert(&unionLL, temp->data); //storing all the data of lLLone in union
temp = temp->next;
}
while(LLTwo != NULL)
{
if(!search(LLOne, LLTwo->data) //if LLone data != LLtwo data then data of llrtwo will insert in union
{
insert(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->next; //LLtwo switching to next
}
return unionLL;
}
struct node* intersection(struct node *LLOne, struct node *LLTwo) //function for intersection
{
intersectionLL = NULL;
while(LLOne != NULL)
{
if(search(LLTwo, LLOne->data))
{ //if LLtwo data & LLone data are same then it will then store the data in intersection
insert(&intersectionLL, LLOne->data);
}
LLOne = LLOne->next; //LLone switching to next data
}
return intersectionLL;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data); // printing the storeed value next to next used in all function like display of union,intersection,
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, LLOneCount, LLTwoCount, temp;
initialize(); // function instialize
printf("Enter number of nodes in first Linked List\n"); //entering the number of node
scanf("%d", &LLOneCount); //storing the value
printf("Enter %d integers\n", LLOneCount); //entering the values for LLone
for(i=0; i<LLOneCount; i++){
scanf("%d", &temp); //scaning the adress of entered value
insert(&LLOne, temp); //insert function for the storing value in LLone
}
printLinkedList(LLOne); //displaying the value stored in LLONE
printf("\nEnter number of nodes in second Linked List\n"); //entering the number of node
scanf("%d", &LLTwoCount); //storing the value
printf("Enter %d integers\n", LLTwoCount); //entering the values for LLtwo
for(i=0; i<LLTwoCount; i++)
{
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo); //printing LLtwo data stored in the linkedlist
findunion(LLOne, LLTwo ) //union function
intersection(LLOne, LLTwo); //intersection function
printf("\nUnion Linked List\n");
printLinkedList(unionLL); //printing union list
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL); //printing intersection list
return 0;
}