Challenge

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

Embedded live batch 12

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

Set theory is the mathematical theory of well-determined collections, called sets, of objects that are called members, or elements, of the set.

-Two sets

-Union of the two sets

-Intersection of two sets

Code-

#include<stdio.h>
#include<stdlib.h>

//Initializing nodes
struct Node* head1= NULL;
struct Node* head2= NULL;
struct Node* intersect=NULL;
struct Node* uni= NULL;


struct Node                     //Creating a node
{
	int value;
	struct Node *next;
};



void insert_node(struct Node**head, int n) //Function to insert node
 {
     struct Node*node;

     node=malloc(sizeof(struct Node));

     node->value=n;

     node->next=*head;

     *head=node;
  }

void display (struct Node *p)   //printing both the set values
    {
		while (p != NULL)
		{
			printf("%d \n", p->value);
			p = p->next;
		}
	}


int compare (struct Node*head,int n)  //function to compare the two sets
	{
		while(head!=NULL)
		{
		if (head->value==n)
			return 1;
		head=head->next;
	    }
	return 0;
	}


struct Node* get_union(struct Node* head1,struct Node* head2)  //function to get the union
	{
		uni= NULL;
		struct Node*p1 = head1;
		while (p1!=NULL)
		{
			insert_node(&uni,p1->value);
			p1=p1->next;
		}
		while (head2!=NULL)
		{
			if(!compare(head1, head2-> value))
			{
				insert_node(&uni,head2->value);
			}
		head2=head2->next;
	    }
	    return uni;
    }


struct Node*get_intersection(struct Node*head1,struct Node*head2)    //function to get intersection
	{
		intersect=NULL;
		while(head1!=NULL)
		{
			if(compare(head2,head1->value))
			{
				insert_node(&intersect,head1->value);
			}
			head1=head1->next;
		}
		return intersect;
	}


int main()

	{

	int a,b,c,i;

	printf("Enter the number of nodes :\n");             //Create set1

	fflush (stdout);

	scanf("%d",&a);


	printf("Enter the elements of set1:\n");

	fflush (stdout);

	for (i=0;i<a;i++)
	{

	scanf("%d",&c);

	insert_node(&head1,c);                             //Calling the function to insert nodes

	}
	display(head1);                                  //Calling the function to display




	printf("Enter the number of nodes :\n");         //Create set2

	fflush (stdout);

	scanf("%d",&b);


	printf("Enter the elements of set2:\n");

	fflush(stdout);


	for (i=0;i<b;i++)

    {
	scanf("%d",&c);

	insert_node(&head2,c);                             //Calling the function to insert nodes

    }

	display(head2);                                   //Calling the function to display



	get_union(head1,head2);                           //Function to get union  of 2 sets

	printf("Union of the two linked lists:\n");

	display(uni);                                 //displaying the union of 2 sets


	get_intersection(head1,head2);                     //Function to get intersection of 2 sets

	printf("Intersection of the two sets :\n");

	display(intersect);                          //displaying the intersection of 2 sets

    return 0;
	}

Output -

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.