Challenge

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

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

#include<stdio.h>

#include<stdlib.h>

typedef struct node //Create a node using structure consists of data and address fields

{

int value;

struct node *next;

};

void nodecreate(int value, struct node **head)

{

struct node *newnode = malloc(sizeof(struct node)); //Create a newnode of struct type & allocate memory

newnode->value=value; //Move the value to newnode

newnode->next=*head; //Assign the address of current newnode in next

*head=newnode; //Make it as head node

}

void print(struct node *node_print)

{

while(node_print!=NULL) //if the head is not equal to null

{

printf("%d\t",node_print->value); //Print the value

node_print=node_print->next; //increment the address to next node

} //Repeat the operation

}

struct node* union_fn(struct node* seta,struct node* setb)

{

int flag;

struct node* result = NULL;

struct node* h1 = seta;

struct node* h2 = setb;

while (h1 != NULL) //Loop to copy the value of set A to Union Set

{

nodecreate(h1->value,&result); //Copy the value to union LL

h1=h1->next; //Increase the address and repeat the operation

}

h1=seta; //Reassign the set A header to h1

while (h2 != NULL) //Repeat the loop until h2 is not equal to NULL

{

while(h1!=NULL) //Repeat the loop until h1 is not equal to NULL

{

if (h1->value != h2->value) //h1 value is not equal to h2 value

flag =4; //Assign flag value as 4

else if(h1->value == h2->value)//else assign flag value as 5

{

flag=5;

h1=h1->next; //Increase the address of h1

break; //break the loop

}

h1=h1->next; //Increase the address of h1 if flag=4

}

h1=seta; //Reassign the address of h1 to Set A

if(flag==4) //If flag==4, then copy the value to union LL

nodecreate(h2->value,&result);

h2 = h2->next; //Increment the address of h2

}

return result; //Return the address of the result which points the starting address of union

}

struct node* intersection_fn(struct node* seta,struct node* setb)

{

int flag;

struct node* result = NULL;

struct node* h1 = seta;

struct node* h2 = setb;

while (h2 != NULL) //Repeat the loop until h2 is not equal to NULL

{

while(h1!=NULL) //Repeat the loop until h1 is not equal to NULL

{

if (h1->value != h2->value)//h1 value is not equal to h2 value

flag =4; //Assign flag value as 4

else if(h1->value == h2->value)

{

flag=5; //else assign flag value as 5

h1=h1->next; //Increase the address of h1

break; //break the loop

}

h1=h1->next; //Increase the address of h1 if flag=4

}

h1=seta; //Reassign the address of h1 to Set A

if(flag==5) //If flag==5, then copy the value to intersection LL

nodecreate(h2->value,&result);

h2 = h2->next; //Increment the address of h2

}

return result; //Return the address of the result which points the starting address of intersection

}

int main()

{

int i,n1,n2;

int set_a[50],set_b[50];

struct node *head_a=NULL;

struct node *head_b=NULL;

struct node *head_u=NULL;

struct node *head_i=NULL;

printf("Enter the Number of elements in Set A:\n"); //Receive number of elements in LL

fflush(stdout);

scanf("%d",&n1);

printf("Enter the element:\n"); //Receive the element and store in an array

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

{

fflush(stdout);

scanf("%d",&set_a[i]);

nodecreate(set_a[i],&head_a); //Create a node for each element in an array

}

printf("Enter the Number of elements in Set B:\n"); //Receive number of elements in LL

fflush(stdout);

scanf("%d",&n2);

printf("Enter the element:\n"); //Receive the element and store in an array

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

{

fflush(stdout);

scanf("%d",&set_b[i]);

nodecreate(set_b[i],&head_b); //Create a node for each element in an array

}

//Function Call to print Received elements

printf("Set A Elements:\n");

print(head_a);

printf("\nSet B Elements:\n");

print(head_b);

//Union Function Call

head_u=union_fn(head_a,head_b);

printf("\nUnion of Set A & Set B Elements are:\n");

print(head_u);

//Intersection Function Call

head_i=intersection_fn(head_a,head_b);

printf("\nIntersection of Set A & Set B Elements are:\n");

print(head_i);

}

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.