/*
* project1.c
*
* Created on: 22-Nov-2022
* Author: rafi
*/
#include
#include
struct node {
int data;
struct node *next;
} *list1, *list2, *unionlist, *intersectionlist;
void initialize()
{
list1 = list2 = NULL;
}
void insert(struct node **head, int n) {
// Creating a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = n;
newNode->next = *head;
*head = newNode;
}
int search(struct node *head, int n)
{
//here i am writing a function for searching every element of the node by traversing from start to end
while (head != NULL)
{
if (head->data == n)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* findunion(struct node *list1, struct node *list2) {
unionlist = NULL;
struct node *temp = list1; /*here i am inserting all nodes of list1 to unionlist through *temp*/
while(temp != NULL)
{
insert(&unionlist, temp->data);
temp = temp->next;
}
while(list2 != NULL){
if(!search(list1, list2->data)){ /*here i am comparing nodes of list 1&2 by search function and entering nodes that are
not present in list1 to unionlist */
insert(&unionlist, list2->data);
}
list2 = list2->next;
}
return unionlist;
}
struct node* intersection(struct node *list1, struct node *list2)
{
intersectionlist = NULL;
while(list1 != NULL)
{
if(search(list2, list1->data))
{ /* here i am comparing list1 data with list2 and entering the nodes that are common in intersectionlist*/
insert(&intersectionlist, list1->data);
}
list1 = list1->next;
}
return intersectionlist;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL) {
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, list1Count, list2Count, temp;
initialize();
printf("Enter number of nodes in first Linked List\n");
fflush(stdout); //creating nodes for list 1
scanf("%d", &list1Count);
printf("Enter %d integers\n", list1Count);
for(i=0; i