Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

AIM :- Create a C program to find the union and intersection of two linked list

OBJECTIVE :-

  • Create two linked list
  • Find Union of linked list
  • Find Intersection of two linked list

C CODE :-

// In this program we will find the union and intersection of two linked list
#include  // Processor Selection
#include 
// Here we had created the structure of node that contains the pointer and data variable
struct node
{
	int data;
	struct node *next;
};

struct node *head1,*head2,*newnode1,*newnode2;
struct node *temp1,*temp2;

// This function creates the linked list 1 on the basis of data provided by user
void create1()
{
	int i = 0, choice;
	printf("\n How many node do you want to create in linked list 1 = ");
	fflush(stdout);
	scanf("%d",&choice);
	head1 = 0;
	while(i < choice)
	{
	newnode1 = (struct node *)malloc(sizeof(struct node));
	printf("\n Enter the number = ");
	fflush(stdout);
	scanf("%d",&newnode1 ->data);
	newnode1 ->next=0;
	if(head1 == 0)
	{
		head1 = temp1 = newnode1;
	}
	else
	{
		temp1 ->next = newnode1;
		temp1 = newnode1;
	}
	 i = i +1;
    }
}

// This function is created to display the created linked list 1
void display1()
{
	temp1 = head1;
	while(temp1 != 0)
	{
		printf(" %d ",temp1 ->data);
		temp1 = temp1 ->next;
	}
}

// This function creates linked list two on the basis of data provided by user
void create2()
{
	int i = 0, choice;
	printf("\n How many node do you want to create in linked list 2 = ");
	fflush(stdout);
	scanf("%d",&choice);
	head2 = 0;
	while(i < choice)
	{
	newnode2 = (struct node *)malloc(sizeof(struct node));
	printf("\n Enter the number = ");
	fflush(stdout);
	scanf("%d",&newnode2 ->data);
	newnode2 ->next = 0;
	if(head2 == 0)
	{
		head2 = temp2 = newnode2;
	}
	else
	{
		temp2 ->next = newnode2;
		temp2 = newnode2;
	}
	 i = i + 1;
    }
}

// This function is created to display the linked list 2
void display2()
{
	temp2 = head2;
	while(temp2 != 0)
	{
		printf(" %d ",temp2 ->data);
		temp2 = temp2 ->next;
	}
}


struct node *newnode3,*curr1,*curr2,*temp3,*head3,*temp4;

/* This check1 function will create the union of two linked list */
void check1()
{
    curr1 = head1; // Curr1 will store the address of first element of linked list 1
    curr2 = head2; // Curr1 will store the address of first element of linked list 2
    head3 = 0;     // head3 will store the adress of first element of union's linked list

    /* Here we are creating new linked list and
    all the elements in the linked 1 will be transferred in that linked list first */
    while(curr1 != 0)
    {
        newnode3 = (struct node *)malloc(sizeof(struct node));
        newnode3->data = curr1->data;
        newnode3->next = 0;
        if(head3 == 0)
        {
            head3 = temp3 = newnode3;
        }
        else
        {
            temp3->next = newnode3;
            temp3 = newnode3;
        }
        curr1 = curr1->next;
    }

    // Now we are going to add the linked list 2 element in the union linked list
    while(curr2 != 0)
    {
        int found = 0;
        temp4 = head3;
        while(temp4 != 0)
        {
            if (temp4->data == curr2->data) //This if loop avoids the repetation of number
            {
                found = 1;
                break;
            }
            temp4 = temp4->next;
        }
        if (found == 0)
        {
            newnode3 = (struct node *)malloc(sizeof(struct node));
            newnode3->data = curr2->data;
            newnode3->next = 0;
            if (head3 == 0)
            {
                head3 = temp3 = newnode3;
            }
            else
            {
                temp3->next = newnode3;
                temp3 = newnode3;
            }
        }
        curr2 = curr2->next;
    }
}

// This function will display the union linked list
void display3()
{
	temp3 = head3;
	while(temp3 != 0)
	{
		printf(" %d ",temp3 ->data);
		temp3 = temp3 ->next;
	}
}

struct node *newnode4,*temp5,*head4;

// This function will find the intersection of two linked list
// This function also create seprate linked list for intersection elements
void intersect()
{
	curr1 = head1;
	head4 = 0;
	while(curr1 != 0)
	{
		curr2 = head2;
		while(curr2 != 0)
		{
			if((curr1 ->data) == (curr2 ->data))
			{
				newnode4 = (struct node *)malloc(sizeof(struct node));
				newnode4 ->data = curr1 ->data;
				newnode4 ->next = 0;
				if(head4 == 0)
				{
					head4 = temp5 = newnode4;
				}
				else
				{
					temp5 ->next=newnode4;
					temp5 = newnode4;
				}
				curr2 = curr2 ->next;
			}
			else
			{
				curr2 = curr2 ->next;
			}
		}
		curr1 = curr1 ->next;
	}
}

// This function will display the intersection linked list
void display4()
{
	temp5 = head4;
	if(temp5 == 0)
	{
		printf("\n No Intersection ");
	}
	while(temp5 != 0)
	{
		printf(" %d ",temp5 ->data);
		temp5 = temp5 ->next;
	}
}


int main()
{
	printf("\n In this program we will find the Intersection and Union of two linked list\n");
	create1(); // Calling create1 function to create linked list
	printf("\n First linked list is :-\n");
	display1(); // Calling display function to display linked list1
	create2(); // Calling create2 function to create linked list
	printf("\n Second linked list is :-\n");
	display2(); // Calling display function to display linked list2
	printf("\n The union linked list is :- \n");
	check1(); // Calling check1 function to create union linked list
	display3(); // Calling display function to display union linked list
	printf("\n The intersection of two linked list is :-\n");
	intersect(); // Calling intersection function to create intersection linked list
	display4(); // Calling display function to display intersection linked list
	// Free up all the pointers using free command
	free(newnode1);
	free(newnode2);
	free(newnode3);
	free(newnode4);
	return 0;
}

OUTPUT :-

CONCLUSION :- We had created the C program in which we can able to make two linked list and also finding the Union and Intersection of two linked list part also implemented.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.