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 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List//

* compare.c
*
* Created on: Jul 10, 2023
* Author: shant
*/
#include"set_theory.h" // including the set_theory.h header file
int compare(struct NODE *c_node, int n) // definiton of compare function
{
while(c_node !=NULL) { //using while loop to iterate through list
if(c_node->data == n) //using if loop to check whether value mathched with second argument passed
{
return 1; //if true then return 1
}
c_node=c_node->next; // update address of next node
}
return 0;
}

#include"set_theory.h" // including the set_theory.h header file
void display(struct NODE *n) // definition of display function
{
while(n!=NULL) { //using while loop to iterate through list
printf("%d\t", n->data); //printing data in list
n=n->next; // update address of next node
}
printf("\n"); //printing with new line
}

#include"set_theory.h" // including the set_theory.h header file
struct NODE *insert_node(struct NODE *head,int data) // definition of insert node function
{
//printf("inserting data in begining\n"); // printing new node inserted at begining
struct NODE *newnode=(struct NODE *)malloc(sizeof(struct NODE)); // declare new node pointer and allocate space to new node by malloc function
//printf("adress of new node is:%p\n",newnode);
newnode->data=data; //store data in new node by user
//printf("data inserted in newnode is:%d\n",newnode->data); //printing data in new node given by user
newnode->next=head; //assign head to newnode
head=newnode;
return head; //return new reference of address list
}

#include"set_theory.h" // including the set_theory.h header file
struct NODE *get_intersection(struct NODE *head1,struct NODE *head2) //definition of *get_intersection function with two argument and retrun
{
struct NODE *head_intersection=NULL; //create new pointer head_intersection
while(head1 !=NULL) //using while loop to iterate through 1st list
{
if(compare(head2, head1->data)) // using compare function to check whether common value is there.
{
head_intersection=insert_node(head_intersection, head1->data); //if true,then insert data in head intersection
}
head1=head1->next; //update address of head1
}
return head_intersection; //returning the address of new list
}

// main.c
#include"set_theory.h" // including the set_theory.h header file
int main() //main program
{
int i;
struct NODE *head1 =NULL; // creating head pointer1 of struct node
struct NODE *head2 =NULL; // creating head pointer1 of struct node
struct NODE *res_union= NULL; // creating res_union pointer1 of struct node
struct NODE *res_intersection= NULL; // creating res_intersection pointer1 of struct node
int first_list_nodes=0; //declare variable from user for sorting
int second_list_nodes=0;
int first_list_item=0;
int second_list_item=0;
printf("enter the no of nodes in 1st list:"); //asking the user size of 1st list
fflush(stdout); // clearing o/p stream
scanf("%d",&first_list_nodes); // store value in 1st list node
for( i=0; i<first_list_nodes; i++)
{
printf("enter the data %d of first list",i+1); //asking user to print data in first list
fflush(stdout);
scanf("%d", &first_list_item); // store in first list item
head1=insert_node(head1,first_list_item); // calling the insert node function to insert the value from user in new node and getting address of new node
}
printf( "the data inside list1 is :\n"); // print data in 1st list by user
display(head1); //calling display function to print list 1.
printf("enter the no of nodes in 2nd list:"); //asking the user size of 2nd list
fflush(stdout); // clearing o/p stream
scanf("%d",&second_list_nodes); // store value in 2nd list node
for( i=0; i<second_list_nodes; i++) // using for loop to get data for each node
{
printf("enter the data %d of second list",i+1); //asking user to print data in first list
fflush(stdout); // clearing o/p stream
scanf("%d", &second_list_item); // store in first list item
head2=insert_node(head2,second_list_item); // calling the insert node function to insert the value from user in new node and getting address of new node
}
printf( "the data inside list2 is :\n"); // print data in 1st list by user
display(head2); //calling display function to print list 1.
// calling the get_intersection function to get common value in both list with passing argument head1 and head2
res_intersection= get_intersection(head1,head2); //updating address of result in res_intersection
printf("the list of intersection is:\n"); //print common vale of both list
display(res_intersection); // calling display funtion print result
// calling the get_union function to get all unique value in both list with passing argument head1 and head2
res_union=get_union(head1,head2); //updating address of result in res_union
printf("the list of union is :\n"); // print data in 1st list by user
display(res_union); //calling display function to print result
return 0;
}
#ifndef SET_THEORY_H_ //undefining the name if already defined
#define SET_THEORY_H_ // deine the name of library
#include<stdio.h> // pre- processor directive
#include<stdlib.h> // adding library to use it
struct NODE // create node to use in linked list
{
int data; // declare first member is data
struct NODE *next; // declare 2nd member to store address of next node
};
struct NODE *insert_node(struct NODE *, int); //declare insertNode function with two argument and retrun
struct NODE *get_intersection(struct NODE *,struct NODE *); //declare *get_intersection function with two argument and retrun
struct NODE *get_union(struct NODE *,struct NODE *); //declare *get_union function with two argument and retrun
void display(struct NODE *); //declare display function with no return
int compare(struct NODE *head, int n); // declare compare function with two argument and return
#endif /* SET_THEORY_H_ */ // end of library
#include"set_theory.h" // including the set_theory.h header file
struct NODE *get_union(struct NODE *head1,struct NODE *head2) //definition of get_union function with two argument and retrun
{
struct NODE *head_union=NULL; //create new pointer head_union
while(head1!=NULL) //using while loop to iterate through 1st list
{
head_union=insert_node(head_union, head1->data); // insrt all unique value of head union with head1
head1=head1->next; // update address pf head1
}
while(head2!=NULL) //using while loop to iterate through 2st list
{
if(!compare(head_union, head2->data)) // using compare function to check whether common value is there.
{
head_union=insert_node(head_union, head2->data); //if true,then insert data in head union
}
head2=head2->next; //update address of head2
}
return head_union; //returning the address of new list
}

//Makefile

all: main.c intersection.c union.c display.c compare.c set_theory.h insert_node.c
gcc main.c intersection.c union.c display.c compare.c set_theory.h insert_node.c -o main -I.

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.