Challenge

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

Answer :-

MATHEMATICAL SET THEORY:-

Set Theory is the branch of mathematical logic that studies sets, which can be informally described as collections of objects.

A venn diagram illustrating the intersection of two sets.

Union of the sets A and B, denoted A U B, is the set of all objects that are a member of A or B or both. For ex, the union of {1,2,3} and {2,3,4} is the set {1,2,3,4}.

Intersection of the sets A and B, denoted A ∩ B, is the set of all objects that are members of both A and B. For ex, the intersection of {1,2,3} and {2,3,4} is the set {2,3}.

CODE:-

/*
============================================================================
Name : main.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include "list.h" //including header file//

int main() //main function//
{
int i,first_list_nodes=0,first_list_items=0,second_list_nodes=0,second_list_items=0; //initializing integer variables//

printf("enter the number of nodes in first list:"); //printing the message and requesting for input//
fflush(stdout); //clear cache memory//
scanf("%d",&first_list_nodes); //reads and stores the data//

printf("enter %d the elements:",first_list_nodes); //printing message to enter first list elements//
for(i=0;i<first_list_nodes;i++) //for loop to scan the first list elements//
{
fflush(stdout); //clear cache memory//
scanf("%d",&first_list_items); //reads and stores the data//
head1=insert_node(head1,first_list_items); //calling insert node function//
}
display_list(head1); //display function called to display the elements//

printf("\n\nenter the number of nodes in second list:"); //printing the message and requesting for input//
fflush(stdout); //clear cache memory//
scanf("%d",&second_list_nodes); //reads and stores the number of datas//

printf("enter %d the elements:",second_list_nodes); //print the message and request the second list//
for(i=0;i<second_list_nodes;i++) //for loop to scan the second list elements//
{
fflush(stdout); //clear cache memory//
scanf("%d",&second_list_items); //reads and stores the data//
head2=insert_node(head2,second_list_items); //calling insert node function//
}
display_list(head2); //display function called to display the elements//

printf("\n"); //next line//
res_union=get_union(head1,head2); //calling function result of union//
printf("\nhere is the result of union:"); //print the message//
display_list(res_union); //display function to display the elements of union//

printf("\n");
res_intersection=get_intersection(head1,head2); //calling function result of intersection//
printf("\nhere is the result of intersection:"); //print the message//
display_list(res_intersection); //display function called to display the elements of intersection//
}

/*
* list.h
*
* Created on: 25-Apr-2022
* Author: mehul
*/

#ifndef LIST_H_ //header file definition//
#define LIST_H_ //defining list.h header file//
#include <stdio.h> //preprocessor//
#include <stdlib.h> //preprocessor//
#include <stdbool.h> //preprocessor//

typedef struct node //defining the node//
{
int data; //declaring integer type data//
struct node *next; //declaring pointer next in structure node//
}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection; //declaring variables as static node//

Node *insert_node(Node *head_ref,int n); //defining the function for insert node//
void display_list(Node *head_ref); //defining the function for display of list//
Node *get_union(Node *head_pointer1,Node *head_pointer2); //defining the function for get union//
Node *get_intersection(Node *head_pointer1,Node *head_pointer2); //defining the function for intersection of elements//
int compare(Node *head,int data); //defining the function for comparision of elements//


#endif /* LIST_H_ */ //end of list.h header file//

/*
* list.c
*
* Created on: 25-Apr-2022
* Author: mehul
*/

#include "list.h" //including list.h header file//

void display_list(Node *head_ref) //definition of display function//
{
if(head_ref==0) //if condition to check address head_ref is zero//
{
printf("list is empty."); //if condition is true print the message//
}
else //if condition is false continue with else//
{
tail=head_ref; //assigning tail i.e,last data address pushed as head_ref//
while(tail !=0) //while loop to check tail is not equal to 0//
{
printf("%d",tail->data); //print the data entered by user//
tail=tail->next; //assigning tail as next address//
}
tail=newnode; //assigning tail to newnode//
}
}


Node *insert_node(Node *head_ref,int n) //definition of insert node//
{
printf("\ninserting elements at the begining...\n"); //print the message//
newnode=(Node*) malloc(sizeof(Node)); //assigning new address with size//
printf("address of created newnode:%p\n",newnode); //print the address of newnode//
newnode->data =n; //push the data of newnode into n//
printf("inserted value is :%d\n",newnode->data); //printing the data of newnode//
newnode->next = head_ref; //assigning the next address to head_ref//
head_ref = newnode; //assigning the address to newnode//
return head_ref; //return to head reference//
}


Node *get_union(Node *head_pointer1,Node *head_pointer2) //definition of get union//
{
while(head_pointer1 !=0) //while loop to check head pointer1 not equal to 0//
{
head_union = insert_node(head_union,head_pointer1->data); //calling insert node function, printing the data of headpointer1 and saving in head union//
head_pointer1 = head_pointer1->next; //next address//
}
while(head_pointer2 !=0) //while loop to check headpointer2 not equal to 0//
{
if(!compare(head_union,head_pointer2->data)) //if condition and calling compare to check if any data present in union present in headpointer2//
head_union = insert_node(head_union,head_pointer2->data); //if the data of headpointer2 not present in head union then copy it in head union//
head_pointer2 = head_pointer2->next; //else go to next data//
}
return head_union; //return program to head_union//
}


int compare(Node *head,int data) //definition of compare function//
{
while (head != NULL) //while loop to check head not equal to null//
{
if(head->data == data) //if condition to check head of data equal to data//
return 1; //if condition is true return that value//
head = head->next; //else assign head to next address//
}
return 0; //return to main program//
}


Node *get_intersection(Node *head_pointer1,Node *head_pointer2) //definition of intersection function//
{
while(head_pointer1 !=0) //while loop to check headpointer1 not equal to 0//
{
if (compare(head_pointer2, head_pointer1->data)) //if condition and calling compare to get common elements//
head_intersection = insert_node(head_intersection,head_pointer1->data); //if condition is true calling insert node function and assigning the data into intersection//
head_pointer1 = head_pointer1->next; //else going to next address//
}
return head_intersection; //return to head intersection in main function//
}

OUTPUT WINDOW:-

OVERVIEW OF THE PROJECT:-

This program gives the overall view of the mathematical set theory, which helps us to find a union from a set of datas. It also helps us to find a intersection elements from the set of datas. The program helps us to get a sufficient knowledge of the c programming language. It helps us how to create header files and source files and making the program to look easier. This program has a various functions, conditional functions and several methods to create the program. This program helps us how to call a function and how to terminate the function.

GOALS:-

  1. The implementation of interface for mathematical set theory using c programming is done which is showed in the code.
  2. The set of datas are given and the union and intersection of these sets has been shown in output window.(image address:-https://sklc-tinymce-2021.s3.amazonaws.com/comp/2022/04/Screenshot%20(145)_1650970574.png)
  3. The two sets given are {1,2,3,4} and {2,3,4,5} the union of these sets we got is {1,2,3,4,5} and intersection of sets we got is {2,3,4}.

Key Highlights of the Project:-

  • The creation of 2 lists of data using linked list has been achieved in the program.(refer the screenshots attached)
  • The insert node function is used to insert the node 1st and 2nd list accordingly.
  • The intersection of data of two list is done by using the intersection function which operates and provides the output.
  • The union of two list is achieved by using the function union which helps to get the result.

Deliverables of this project:-

  • Intersection of data of two lists is shown below in the screenshot :
  • The two sets {1,2,3,4} and {5,4,3,2} has common elements of {2,3,4}, hence the result we got is 234.
  • The two sets {1,2,3,4} and {5,4,3,2} whose union is the elements of both the sets {1,2,3,4,5} is the result.
  • Union of data of two lists is shown below in the screenshot :

OUTCOMES OF THE PROGRAM:-

  • The program helps us to gain a knowledge of creating the nodes using linked list.
  • It helps us to perform the union of two sets or data of two lists.
  • It also helps us to find the intersection of two sets or data of two lists.
  • It also helps us to compare the elements of two lists or two sets.
  • It also helps us to display the elements of union and intersection using different methods.
  • The program is very interesting and enjoy executing it.
  • The required documents and files of the program are uploaded for the reference.
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.