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-

In this project, interfaces for mathematical Set Theory are implemented using C programming language. Users can give inputs like two lists of data and this program will give the output as the union and intersection of these two lists

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

struct node
{
int num;
struct node *next;
};
//function declaration
void createlist(struct node **, int);
void display(struct node *);

// function that returns true if data is present in the linked list else return false
bool compare(struct node* temp,int num)
{
struct node* t = temp;
while (t != NULL)
{
if (t->num == num)
return 1;
t = t->next;
}
return 0;
}


// Function to find union of two linked lists list1 and list2
struct node* findUnion(struct node *list1, struct node *list2)
{
struct node *a=list1, *b=list2;
struct node *result=NULL;
// Insert all elements of list1 to the result list
while(a!=NULL)
{

createlist(&result,a->num);
a = a->next;
}
// Insert those elements of list2 which are not present in result list1-
while (b!= NULL)
{
if (!compare(result, b->num))
{
createlist(&result, b->num);
}
b = b->next;
}
return result;
}


// Function to find intersection of two linked lists head1 and head2
struct node* findintersection(struct node *list1, struct node *list2)
{

struct node* result = NULL;
struct node* a = list1 ;

// Traverse list1 and search for each element of it in list2. If the element is present in list 2, then
// insert the element as a result
while (a != NULL) {
if (compare(list2, a->num))
{
createlist(&result, a->num);
}
a = a->next;
}

return result;

}
// function to create a linked list
void createlist(struct node** head, int temp)
{
struct node* fnnode;
// dynamic memory allocation of node
fnnode = (struct node *)malloc(sizeof(struct node));
//put payload that is num
fnnode->num = temp;
//link the old list to the new node that is fnnode
fnnode->next = (*head);
//move the address of the head to the fnnode
(*head)=fnnode;
}
// function to display a linked list
void display(struct node* list)
{
while (list != NULL)
{
printf("%d",list->num);
list = list->next;
}
}
int main()
{
int n,m,node;
printf("Enter the nodes for List1 and List2:");
fflush(stdout);
scanf("%d %d",&n,&m);
//Start structure pointer with empty list
struct node* list1 = NULL;
struct node* list2 = NULL;


//for loop for link list1
for(int i=0;i<n;i++)
{
printf("\nEnter the value in linked list1 %d:-",i);
fflush(stdout);
scanf("%d",&node);
createlist(&list1,node);//calling function to create nodes for linked list1

}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("\nEnter the value in linked list2 %d:-",i);
fflush(stdout);
scanf("%d", &node);
createlist(&list2, node);//calling function to create nodes for linked list2

}

printf("\n Display list1:");
display(list1); //calling function with structure pointer as argument
printf("\n Display list2:");
display(list2); //calling function with structure pointer as argument
printf("\n Union of both lists:");

display(findUnion(list1, list2)); //calling function with structure pointer as argument
printf("\n Intersection of both lists:");

display(findintersection(list1, list2));//calling function with structure pointer as argument
return 0;
}

.C file- file:///C:/Users/dipal/Desktop/project1.txt

Output 1-

Enter the nodes for List1 and List2:4 3

Enter the value in linked list1 0:-1

Enter the value in linked list1 1:-3

Enter the value in linked list1 2:-4

Enter the value in linked list1 3:-5

Enter the value in linked list2 0:-3

Enter the value in linked list2 1:-5

Enter the value in linked list2 2:-6

Display list1:5431
Display list2:653
Union of both lists:61345
Intersection of both lists:35

Output 2-

Enter the nodes for List1 and List2:2 4

Enter the value in linked list1 0:-6

Enter the value in linked list1 1:-8

Enter the value in linked list2 0:-4

Enter the value in linked list2 1:-6

Enter the value in linked list2 2:-9

Enter the value in linked list2 3:-7

Display list1:86
Display list2:7964
Union of both lists:49768
Intersection of both lists:6

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.