Challenge

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

/* Overview of the Project:
In this project, interfaces for mathematical Set Theory are implemented using C programming language.
User can give the input like two list of data and this program will give the output as the union and intersection of these two lists.
In this project we have created the two list of data using linked list method.
Then we find the union and intersection of the same. */

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first_list,*second_list,*union_list,*intersection_list;

void initialize()
{
first_list=second_list= NULL;
}

void insert(struct node** head_ref, int new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}

int traverse(struct node *head_ref, int new_data)
{
while (head_ref != NULL)
{
if (head_ref->data == new_data)
{
return 1;
}
head_ref = head_ref->next;
}
return 0;
}

struct node* Union(struct node *first_list, struct node *second_list)
{
union_list = NULL;
struct node *temp = first_list;
while(temp != NULL)
{
insert(&union_list, temp->data);
temp = temp->next;
}
while(second_list != NULL)
{
if(!traverse(first_list, second_list->data))

{
insert(&union_list, second_list->data);
}
second_list = second_list->next;
}
return union_list;
}

struct node* intersection(struct node *first_list, struct node *second_list)
{
intersection_list = NULL;
while(first_list != NULL)
{
if(traverse(second_list, first_list->data))
{
insert(&intersection_list, first_list->data);
}
first_list = first_list->next;
}
return intersection_list;
}

void final_list(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("->");
}
}

int main()
{
int i, num1, num2, temp;
initialize();
printf("Set 1\n");
printf("Enter the number of elements .\n");
scanf("%d", &num1);
printf("Enter %d elements\n", num1);

for(i=0; i<num1; i++)
{
scanf("%d", &temp);
insert(&first_list, temp);
}
final_list(first_list);
printf("\n\nSet 2\n");
printf("Enter the number of elements.\n");
scanf("%d", &num2);
printf("Enter %d elements\n", num2);

for(i=0; i<num2; i++)
{
scanf("%d", &temp);
insert(&second_list, temp);
}
final_list(second_list);
Union(first_list,second_list);
intersection(first_list, second_list);
printf("\n\nUnion of the set is :\n");
final_list(union_list);
printf("\n\nIntersection of the set is :\n");
final_list(intersection_list);

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.