Challenge

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

Mathematical set theory

Objective: To create a c program to find the union and intersection of of two sets using linked list.

Program:

set.h

#ifndef SET_H_
#define SET_H_
#include <stdio.h>
#include <stdlib.h>

struct node { //creating strucure
int data; //declaring integer type for the linkedlist data
struct node *next; //declaring pointer as struct for address of linkedlist
} *L1, *L2, *Union, *Intersection; //Declaring variables as struct node pointer

void insert(struct node* *head, int num); //To insert the data to the linkedlist
int search(struct node *head, int num); //To avoid repeatation in union
struct node* findunion(struct node *L1, struct node *L2); //To calculate union
struct node* intersection(struct node *L1, struct node *L2); //To calculate intersection
void print(struct node *p); //To print the data

#endif /* SET_H_ */

set.c

#include"set.h"

int search(struct node *head, int num) //Function to check the data in the arguments
{
while (head != NULL)
{
if (head->data == num)
{ return 1; }
head = head->next;
}
return 0;
}

void insert(struct node* *head, int num) //Function to insert the data in new node
{
struct node* new = (struct node*) malloc(sizeof(struct node)); //Allocating memeory
new->data = num;
new->next = *head;
*head = new;
}


struct node* findunion(struct node *L1, struct node *L2) //Function to calculate union
{
Union = NULL;
struct node *temp = L1;
while(temp != NULL)
{
insert(&Union, temp->data); //calling insert function
temp = temp->next;
}
while(L2 != NULL)
{
if(!search(L1, L2->data)) //checking the value to not get repeated using search function
{ insert(&Union, L2->data); } //calling insert function
L2 = L2->next;
}
return Union; //returning union as struct node type
}

struct node* intersection(struct node *L1, struct node *L2) //Function to calculate intersection
{
Intersection = NULL;
while(L1 != NULL)
{ if(search(L2, L1->data)) //checking the value to not get repeated using search function
{ insert(&Intersection, L1->data); } //calling insert function
L1 = L1->next;
}
return Intersection; //returning intersectionn as struct node type
}

void print(struct node *p) //Function to print the data
{
while (p != NULL)
{ printf("%d", p->data);
p = p->next;
if(p != NULL)
{ printf(" "); }
}
}

main.c

#include"set.h"

int main()
{
int i, n1, n2, temp; //declaring variables as integer type
L1 = L2 = NULL; //initializing list 1 and 2 to null

printf("Enter number of nodes in first set:\n");
fflush(stdout);
scanf("%d", &n1); //To get the number of elements in set 1
printf("Enter %d integers of first set\n", n1);
for(i=0; i<n1; i++)
{
fflush(stdout);
scanf("%d", &temp); //To get the elements in the set 1
insert(&L1, temp); //To insert the elements in set 1
}
print(L1); //To print the elements in set 1

printf("\nEnter number of nodes in second set:\n");
fflush(stdout);
scanf("%d", &n2); //To get the number of elements in set 2
printf("Enter %d integers of second set\n", n2);
for(i=0; i<n2; i++)
{
fflush(stdout);
scanf("%d", &temp); //To get the elements in the set 2
insert(&L2, temp); //To insert the elements in set 2
}
print(L2); //To print the elements in set 2

findunion(L1, L2); //calling function to calculate union
intersection(L1, L2); //calling function to calculate intersection
printf("\n\nUnion Linked List\n");
print(Union);
printf("\n\nIntersection Linked List\n");
print(Intersection);

return 0;
}

Makefile

all:main.c set.c set.h
gcc main.c set.c set.h -o set -I.

Output:

Conclusion: Thus the c program to find the union and intersection of two sets using linked list was done successfully and the required output is obtained.

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.

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.