Challenge

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

Aim: To implement the mathematical set theory using C programming language. we will give two input data set like link list and  union & intersection of the two set will be displayed.

C Program:

 #include 
#include 

// structure of linked list
struct node{
	int data;
	struct node *next;

}*linklistone, *linklisttwo, *unionll, *intersectionll;

void initialise(){
linklistone=linklisttwo=NULL;
}


//function used to insert the num into list
void insert (struct node **head, int num)
{
	//we allocate node
	struct node *newnode = (struct node*) malloc (sizeof(struct node));

	//put data
	newnode->data = num;

	//new node as header
	newnode->next = *head;

	// head pointer to new node
	*head = newnode;

}

//searching the element by tranversal
int search(struct node *head,int num)
{
	while(head!=NULL)
	{
		if(head->data==num)
		{return 1;
		}
		head=head->next;
	}
	return 0;

}

//union of two link list
struct node* findunion (struct node *linklistone, struct node *linklisttwo)
	{
	unionll =NULL;
//temping adding linklist one node to unionll
struct node *temp = linklistone;
while(temp != NULL)
{
	insert(&unionll, temp->data);
	temp = temp->next;
}

//insert the nodes from linklist two that are not in linklistone
while(linklisttwo != NULL){
	if(!search(linklistone, linklisttwo->data)){
		insert(&unionll, linklisttwo->data);
	}
	linklisttwo= linklisttwo->next;
}
return unionll;
}

//finding intersection of the two link list
struct node* intersection(struct node *linklistone, struct node *linklisttwo){
	intersectionll = NULL;
	while(linklistone !=NULL)
	{
		if (search(linklisttwo, linklistone->data)){
				insert(&intersectionll, linklistone->data);
	}
		linklistone = linklistone-> next;
	}
	return intersectionll;
}


//function used to print the link list
void printLL(struct node *nodeptr)
{
	while(nodeptr!=NULL){
		printf("%d",nodeptr->data);

	    nodeptr=nodeptr->next;
	if (nodeptr!=NULL)
		printf("--->");
	}
}

int main()
{
	int i, linklist1count, linklist2count, temp;

	//creating first set of link list
	printf("Enter no. of nodes in 1st node:\n");
	scanf("%d",&linklist1count);
	printf ("Enter the %d integer\n",linklist1count);
	for(i=0; i

Output:

Conclusion: Hereby, we have developed the mathematical set theory C programming for find union and intersection of the two set.

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.