Challenge

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

Code-

/*
* Project_1.c
*
* Created on: 26-Aug-2022
* Author: Viraj
*/


#include
#include

// A structure of linked list node
struct node

{
int value;
struct node *next;


} *SetOne, *SetTwo, *SetUnion, *SetIntersection;

void initialize()

{
SetOne = SetTwo = NULL;
}


void insert(struct node **head, int num)

{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->value = num;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}

// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *head, int num)

{
while (head != NULL)

{
if (head->value == num)
{
return 1;
}
head = head->next;
}
return 0;

}

//Returns the union of two given linked list

struct node* funcUnion(struct node *SetOne, struct node *SetTwo)

{
SetUnion = NULL;
// Add all nodes of first set
struct node *temp = SetOne;
while(temp != NULL)

{
insert(&SetUnion, temp->value);
temp = temp->next;
}
// Insert those nodes of set 2 which is not present in set 1
while(SetTwo != NULL)

{
if(!search(SetOne, SetTwo->value))

{
insert(&SetUnion, SetTwo->value);
}
SetTwo = SetTwo->next;
}
return SetUnion;
}


//Returns the Linked List which contains common elements from two sets

struct node* intersection(struct node *SetOne, struct node *SetTwo)

{
SetIntersection = NULL;
//Search every element of SetOne in SetTwo, If found then add it to intersection List
while(SetOne != NULL)

{
if(search(SetTwo, SetOne->value))

{
insert(&SetIntersection, SetOne->value);
}
SetOne = SetOne->next;
}
return SetIntersection;
}
//Prints a linked list from head node till tail node

void printSets(struct node *ptr)

{
while (ptr != NULL)

{
printf("%d\t", ptr->value);
ptr = ptr->next;
}
}

int main()

{
int i, n1, n2, temp;
initialize();
//Creating First Set
printf("\nEnter number of elements in first Set:\n");
fflush(stdout);
scanf("%d", &n1);

for(i=0; i
{
printf("Enter The element %d of set 1:",(i+1));
fflush(stdout);
scanf("%d", &temp);
insert(&SetOne, temp);
}

printf("\nThe first set is as follow\n");
printf("-----------------------------------------\n");
printSets(SetOne);

//Creating Second Set
printf("\n\nEnter number of Elements in second set:\n");
fflush(stdout);
scanf("%d", &n2);

for(i=0; i
{

printf("Enter The element %d of set 2:",(i+1));
fflush(stdout);
scanf("%d", &temp);
insert(&SetTwo, temp);
}

printf("\nThe second set is as follow\n");
printf("-----------------------------------------\n");
printSets(SetTwo);

//printing Union of given two sets
funcUnion(SetOne, SetTwo); //Function call
printf("\n\n\nThe Union of sets is as follow\n");
printf("-----------------------------------------\n");
printSets(SetUnion);

//printing intersection of given two sets
intersection(SetOne, SetTwo);
printf("\n\n\nThe Intersection of sets is as follow\n");
printf("-----------------------------------------\n");
printSets(SetIntersection);

return 0;
}

Result 1-

Result 2-

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.