Challenge

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

#include
#include
struct node
{
int data;
struct node *next;
} *LLfirst, *LLsecond, *LLunion, *LLintersection;

//Creating two list of data using linked list method.

void initialize()
{
LLfirst = LLsecond = NULL;
}

//Insert the node to 1st and 2nd list accordingly.


void insert(struct node **head, int num)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = *head;
*head = newNode;
}
int find(struct node *head, int num)
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}

//Getting Union of two list.


struct node* findunion(struct node *LLfirst, struct node *LLsecond)
{
LLunion = NULL;
struct node *temp = LLfirst;
while(temp != NULL)
{
insert(&LLunion, temp->data);
temp = temp->next;
}
while(LLsecond != NULL)
{
if(!find(LLfirst, LLsecond->data))
{
insert(&LLunion, LLsecond->data);
}
LLsecond = LLsecond->next;
}
return LLunion;
}

//Intersection of data of two lists.


struct node* intersection(struct node *LLOne, struct node *LLTwo)
{
LLintersection = NULL;
while(LLfirst != NULL)
{
if(find(LLsecond, LLfirst->data))
{
insert(&LLintersection, LLfirst->data);
}
LLfirst = LLfirst->next;
}
return LLintersection;
}
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)

{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}

//Comparing the elements of two list.


int main()
{
int i, LLfirstCount, LLsecondCount, temp;
initialize();
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLfirstCount);
for(i=0; i<LLfirstCount; i++)
{
scanf("%d", &temp);
insert(&LLfirst, temp);
}
printLinkedList(LLfirst);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLsecondCount);
for(i=0; i<LLsecondCount; i++)
{
scanf("%d", &temp);
insert(&LLsecond, temp);
}
printLinkedList(LLsecond);
//printing Union of two given linked list
findunion(LLfirst, LLsecond);
intersection(LLfirst, LLsecond);
printf("\nUnion Linked List\n");
printLinkedList(LLunion);
printf("\nIntersection Linked List\n");
printLinkedList(LLintersection);
return 0;
}

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.