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:

To write a C program to Create user defined data type to implement the user interfaces for working with ‘Set’ using Linked List, find out the Intersection and Union of two set’s.

Program:

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

typedef struct node //node is declared by using struct function
{
int data;
struct node *next;
}node;

node* insert(node *head, int n) // create set as linked list

{
int temp_data;
for (int i=0;i<n;i++)
{
printf("\nEnter the data of node %d :",i+1);
fflush(stdout);
scanf("%d",&temp_data); //Getting data of each node from user
(head+i)->data=temp_data; //assigning next pointer to next node
if(i==n-1)
{
(head+i)->next=NULL; // assign NULL to last node
}
else
(head+i)->next=head+i+1;
}
return head;
}

void display(node *p) //function to display all nodes
{

while(p!=NULL) //print the nodes till the next pointer is NULL
{
printf(" %d ",p->data);
fflush(stdout);
p=p->next;
}
}

node* intersection(node *head1, int n1, node *head2, int n2) //Display common elements available in the both sets
{
node *result;
result = (node*)calloc((n1>=n2)?n2:n1,sizeof(node));
int c=0;
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
if((head1+i)->data == (head2+j)->data) //compares the data of set 1 with all elements of the set 2
{
(result+c)->data = (head1+i)->data;
(result+c)->next = result+c+1;
c++;
}
}
}
(result+c-1)->next = NULL;
result = (node*)realloc(result,c);
return result;
}

node* union_set(node *head1, int n1, node *head2, int n2) //function to display the set data of 1 and 2
{
node *result1;
result1 = (node*)calloc(n1+n2,sizeof(node));
int c=n1;
for(int i=0;i<n1;i++)
{
(result1+i)->data = (head1+i)->data; //copy set 1 data into union set
(result1+i)->next = result1+i+1;
}
for(int i=0;i<n2;i++) //compare set 2 data and set 1 data to delete the repeating data
{
int H=0;
for(int j=0;j<n1;j++)
{
if((head2+i)->data == (head1+j)->data) // Check the data of two lists
continue;
else
H++;
}
if(H==n1)
{
(result1+c)->data = (head2+i)->data;
(result1+c)->next = result1+c+1;
c++;
}
}
(result1+c-1)->next = NULL;
return result1;
}


int main()
{
node *head[2]; //head pointer and variables are declared as per requirement
int n[2];
for (int i=0;i<2;i++) //creating a set of linked list
{
printf("\nEnter number of nodes in the set %d:",i+1); //getting number of nodes
fflush(stdout);
scanf("%d",&n[i]);
head[i] = (node*)calloc(n[i],sizeof(node)); //Allocate n number f memory dynamically using calloc
head[i] = insert(head[i],n[i]);
}
for (int i=0;i<2;i++) //display the elements of the two sets of list
{
printf("\nThe Elements in the set of list %d are:",i+1);
fflush(stdout);
display(head[i]);
}
node *res_intersection; //creating intersection set using intersection function
res_intersection = intersection(head[0], n[0], head[1], n[1]);
printf("\nThe intersection elements in the set's are:"); //display the intersection elements in the set
fflush(stdout);
display(res_intersection);

node *res_union; //creating union of set's using union function
res_union = (node*)calloc(n[0]+n[1],sizeof(node));
res_union = union_set(head[0], n[0], head[1], n[1]);
printf("\nThe elements of union set are:"); //display the elements in the union set
fflush(stdout);
display(res_union);
}

Output:

Enter number of nodes in the set 1:3

Enter the data of node 1 :1

Enter the data of node 2 :2

Enter the data of node 3 :3

Enter number of nodes in the set 2:3

Enter the data of node 1 :3

Enter the data of node 2 :4

Enter the data of node 3 :5

The Elements in the set of list 1 are: 1 2 3
The Elements in the set of list 2 are: 3 4 5
The intersection elements in the set's are: 3
The elements of union set are: 1 2 3 4 5

Screen print:

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.