Challenge

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


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


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

struct Node
{
int data;
struct Node *next;
}*LLOne, *LLTwo, *ULL, *ILL;

void initialize()
{
LLOne = NULL;
LLTwo = NULL;
ULL = NULL;
ILL = NULL;
}

// Function to insert node in Linked List
void insert(struct Node** address, int element)
{
struct Node *NewNode;
NewNode = (struct Node*)malloc(sizeof(struct Node));
NewNode->data = element;
NewNode->next = *address;

*address = NewNode;
}

// Function to print Linked List.
void printLL(struct Node* address)
{
while(address != NULL)
{
printf("%d \n", address->data);
address = address->next;
}
}
// Function to Search elements of one LLlist in another
int Search(struct Node *LOne ,int common)
{
while(LOne != NULL)
{
if(LOne->data == common)
return (1);
LOne = LOne->next;
}

return (0);
}

// Function to find Intersection of two sets
void Intersection(struct Node *LTwo, struct Node *LOne)
{
int status =0;
while(LTwo != NULL)
{
status = Search(LOne, LTwo->data);
if(status == 1)
printf("%d -->> ",LTwo->data);
LTwo = LTwo->next;
}

/*
ILL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LOne != NULL)

{
if(Search(LTwo, LOne->data))

{
insert(&ILL, LOne->data);
}
LOne = LOne->next;
}
*/
}

// Function to find Union of two sets
void Union(struct Node *LTwo, struct Node *LOne)
{

int status =1;
printLL(LOne);
while(LTwo != NULL)
{
status = Search(LOne, LTwo->data);
if(status == 0)
printf("%d \n ", LTwo->data);
LTwo = LTwo->next;
}

/*
ULL = NULL;
// Add all nodes of first Linked List to unionLL
struct Node *temp = LOne;
while(temp != NULL)

{
insert(&ULL, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(LTwo != NULL)
{
if(!Search(LOne, LTwo->data))
{
insert(&ULL, LTwo->data);
}
LTwo = LTwo->next;
}*/
}

int main()
{
int indexLLOne, indexLLTwo, element,i;
// code for constructing set-1 using linked list
printf("\n Enter number of elements for LLOne : ");
fflush(stdout);
scanf("%d", &indexLLOne);
printf("\n Enter %d elements for LLOne \n ", indexLLOne);

initialize();

for(i=0; i<indexLLOne; i++)
{
fflush(stdout);
scanf("%d", &element);
insert(&LLOne, element);
}

printf("\n Entered %d elements for LLOne are\n ", indexLLOne);
printLL(LLOne);

//Code for Constructing Set-2 using linked list.
printf("\n Enter number of elements for LLTwo : ");
fflush(stdout);
scanf("%d", &indexLLTwo);
printf("\n Enter %d elements for LLTwo \n ", indexLLTwo);

for(i=0; i<indexLLTwo; i++)
{
fflush(stdout);
scanf("%d", &element);
insert(&LLTwo, element);
}

printf("\n Entered %d elements for LLTwo are\n ", indexLLTwo);
printLL(LLTwo);

// union of two set assigned as LLOne and LLTwo respectively
printf("\n Union of both linked list one and two are: \n");
Union(LLTwo, LLOne);
/* printLL(ULL); */

// intersection of two set assigned as LLOne and LLTwo respectively
printf("\n Intersection of both linked list one and two are: \n");
Intersection(LLTwo, LLOne);
/* printLL(ILL); */

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.