
#include
#include
// make a linked list node
struct node
{
int data;
struct node *next;
} *Lone, *Ltwo, *unionll, *intersectionll;
void initialize()
{
Lone = Ltwo = NULL;
}
//Inserts a node in front of a singly lL
void insert(struct node **head, int d)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = d;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}
// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *head, int d)
{
while (head != NULL)
{
if (head->data == d)
{
return 1;
}
head = head->next;
}
return 0;
}
//Returns the union of two given linked list
struct node* findunion(struct node *Lone, struct node *Ltwo)
{
unionll = NULL;
// Add all nodes of first Linked List to unionLL
struct node *t = Lone;
while(t != NULL)
{
insert(&unionll, t->data);
t = t->next;
}
// Insert those nodes of Ltwo which is not present in Lone
while(Ltwo != NULL)
{
if(!search(Lone, Ltwo->data))
{
insert(&unionll, Ltwo->data);
}
Ltwo = Ltwo->next;
}
return unionll;
}
//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *Lone, struct node *Ltwo)
{
intersectionll = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(Lone != NULL)
{
if(search(Ltwo, Lone->data))
{
insert(&intersectionll, Lone->data);
}
Lone = Lone->next;
}
return intersectionll;
}
//Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf(">--->");
}
}
int main()
{
int i, LoneCount, LtwoCount, t;
initialize();
//Creating First linked List
printf("Enter num. of nodes in first LL\n");
scanf("%d", &LoneCount);
printf("Enter %d num. \n", LoneCount);
for(i=0; i