//including library functions//
#include <stdio.h>
#include <stdlib.h>
//creating node structure//
struct node
{
int data;
struct node*next;
};
//creating linked list function//
struct node * createList(int listNum)
{
//declaring structure pointers//
struct node * p = NULL;
struct node * q = NULL;
//declaring variables//
int data,i,n;
//getting input from the user//
printf("\nInput the number of nodes list %d : ",listNum);
fflush(stdout);
scanf("%d", &n);
//allocating memeory for head node using malloc//
struct node * newnode = (struct node *) malloc(sizeof(struct node));
printf("\nEnter value of head node : ");
fflush(stdout);
scanf("%d",&data);
newnode->data = data;
newnode->next = NULL;
p=newnode;//pointer p pointing to head node//
//allocating memory for next nodes//
for(i=1;i<n;i++)
{
q = (struct node *)malloc(sizeof(struct node));
printf("\nEnter value of next node : ");
fflush(stdout);
scanf(" %d", &data);
q->data = data;
q->next = NULL;
//linking head and next nodes//
newnode->next = q;
newnode = newnode->next;
}
return p;
}
//creating union function for list 1& list 2//
struct node * unionlist(struct node*L1, struct node*L2)
{
//declaring pointers//
struct node * output = NULL;
struct node * outTail = NULL;
while(L1&&L2)
{
//allocating memeory for union list//
struct node * newNode = (struct node *) malloc(sizeof(struct node));
newNode->next = NULL;
//comparing values of list 1 and list 2 moving values to newnode//
if(L1->data<L2->data)
{
newNode->data = L1->data;
L1 = L1->next;
}
else if(L1->data>L2->data)
{
newNode->data = L2->data;
L2 = L2->next;
}
else
{
int data = L1->data;
newNode->data = data;
while(L1 && L2 && L1->data == data && L2->data == data)
{
L1 = L1->next;
L2 = L2->next;
}
}
if(!output)
{
//ponting both pointers to newnode//
output = outTail = newNode;
}
else
{
outTail->next = newNode;
outTail = outTail->next;
}
}
//traversing list 1 and adding values to newnode//
while(L1)
{
outTail->next = (struct node *) malloc(sizeof(struct node));
outTail = outTail->next;
outTail->data = L1->data;
L1 = L1->next;
}
//traversing list 2 and adding values to newnode//
while(L2)
{
outTail->next = (struct node *) malloc(sizeof(struct node));
outTail = outTail->next;
outTail->data = L2->data;
L2 = L2->next;
}
outTail->next = NULL;
//returning output for union values//
return output;
}
//creating intersection function for list 1& list 2//
struct node * intersection(struct node * L1, struct node* L2)
{
//declaring pointers//
struct node * output = NULL;
struct node * outTail = NULL;
//traversing list 1& 2//
while(L1&&L2)
{
if(L1->data<L2->data)
{
L1 = L1->next;
}
else if(L2->data<L1->data)
{
L2 = L2->next;
}
else
{
//comparing values of list 1 and list 2 moving values to newnode//
int data = L1->data;
struct node * newNode = (struct node *) malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(output == NULL)
{
outTail = output = newNode;
}
else
{
outTail->next = newNode;
outTail = outTail->next;
}
while(L1 && L2 && L1->data == data && L2->data == data)
{
L1 = L1->next;
L2 = L2->next;
}
}
}
return output;
}
//printing the node values//
void print(struct node * p)
{
if(p == NULL)
{
printf("Empty List\n");
return;
}
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
}
//starting main function//
int main()
{
//declaring function pointers//
struct node * L1 = NULL;
struct node * L2 = NULL;
struct node * L3 = NULL;
struct node * L4 = NULL;
//calling function for creating list//
L1 = createList(1);
L2 = createList(2);
//printing the node values of list 1 &2//
printf("\nvalues of List 1 : ");
print(L1);
printf("\nvalues of List 2 : ");
print(L2);
//printing union list//
printf("\nUnion of list 1 & list 2 : ");
L3 = unionlist(L1, L2);
print(L3);
//printing intersection list//
printf("\nIntersection of list 1 & list 2 : ");
L4 = intersection(L1, L2);
print(L4);
return 0;
}
//including library functions//
#include <stdio.h>
#include <stdlib.h>
//creating node structure//
struct node
{
int data;
struct node*next;
};
//creating linked list function//
struct node * createList(int listNum)
{
//declaring structure pointers//
struct node * p = NULL;
struct node * q = NULL;
//declaring variables//
int data,i,n;
//getting input from the user//
printf("\nInput the number of nodes list %d : ",listNum);
fflush(stdout);
scanf("%d", &n);
//allocating memeory for head node using malloc//
struct node * newnode = (struct node *) malloc(sizeof(struct node));
printf("\nEnter value of head node : ");
fflush(stdout);
scanf("%d",&data);
newnode->data = data;
newnode->next = NULL;
p=newnode;//pointer p pointing to head node//
//allocating memory for next nodes//
for(i=1;i<n;i++)
{
q = (struct node *)malloc(sizeof(struct node));
printf("\nEnter value of next node : ");
fflush(stdout);
scanf(" %d", &data);
q->data = data;
q->next = NULL;
//linking head and next nodes//
newnode->next = q;
newnode = newnode->next;
}
return p;
}
//creating union function for list 1& list 2//
struct node * unionlist(struct node*L1, struct node*L2)
{
//declaring pointers//
struct node * output = NULL;
struct node * outTail = NULL;
while(L1&&L2)
{
//allocating memeory for union list//
struct node * newNode = (struct node *) malloc(sizeof(struct node));
newNode->next = NULL;
//comparing values of list 1 and list 2 moving values to newnode//
if(L1->data<L2->data)
{
newNode->data = L1->data;
L1 = L1->next;
}
else if(L1->data>L2->data)
{
newNode->data = L2->data;
L2 = L2->next;
}
else
{
int data = L1->data;
newNode->data = data;
while(L1 && L2 && L1->data == data && L2->data == data)
{
L1 = L1->next;
L2 = L2->next;
}
}
if(!output)
{
//ponting both pointers to newnode//
output = outTail = newNode;
}
else
{
outTail->next = newNode;
outTail = outTail->next;
}
}
//traversing list 1 and adding values to newnode//
while(L1)
{
outTail->next = (struct node *) malloc(sizeof(struct node));
outTail = outTail->next;
outTail->data = L1->data;
L1 = L1->next;
}
//traversing list 2 and adding values to newnode//
while(L2)
{
outTail->next = (struct node *) malloc(sizeof(struct node));
outTail = outTail->next;
outTail->data = L2->data;
L2 = L2->next;
}
outTail->next = NULL;
//returning output for union values//
return output;
}
//creating intersection function for list 1& list 2//
struct node * intersection(struct node * L1, struct node* L2)
{
//declaring pointers//
struct node * output = NULL;
struct node * outTail = NULL;
//traversing list 1& 2//
while(L1&&L2)
{
if(L1->data<L2->data)
{
L1 = L1->next;
}
else if(L2->data<L1->data)
{
L2 = L2->next;
}
else
{
//comparing values of list 1 and list 2 moving values to newnode//
int data = L1->data;
struct node * newNode = (struct node *) malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(output == NULL)
{
outTail = output = newNode;
}
else
{
outTail->next = newNode;
outTail = outTail->next;
}
while(L1 && L2 && L1->data == data && L2->data == data)
{
L1 = L1->next;
L2 = L2->next;
}
}
}
return output;
}
//printing the node values//
void print(struct node * p)
{
if(p == NULL)
{
printf("Empty List\n");
return;
}
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
}
//starting main function//
int main()
{
//declaring function pointers//
struct node * L1 = NULL;
struct node * L2 = NULL;
struct node * L3 = NULL;
struct node * L4 = NULL;
//calling function for creating list//
L1 = createList(1);
L2 = createList(2);
//printing the node values of list 1 &2//
printf("\nvalues of List 1 : ");
print(L1);
printf("\nvalues of List 2 : ");
print(L2);
//printing union list//
printf("\nUnion of list 1 & list 2 : ");
L3 = unionlist(L1, L2);
print(L3);
//printing intersection list//
printf("\nIntersection of list 1 & list 2 : ");
L4 = intersection(L1, L2);
print(L4);
return 0;
}