Challenge

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

CODE

settheory.h

//defining a header file to store all libraries used for this project
#ifndef SETTHEORY_H_
#define SETTHEORY_H_
//preprocessor directives
#include<stdio.h> //header file for std input and output
#include<stdlib.h> //header file for malloc
struct NODE //node for linked list
{
int data;//data for node
struct NODE *next;//stores address of next node
};
struct NODE *insert_node(struct NODE *,int); //declaring insert_node function with return and 2 arguements
struct NODE *get_intersection(struct NODE *,struct NODE *); //declaring get_intersection function with return and 2 arguements
struct NODE *get_union(struct NODE *,struct NODE *); //declaring get_union function with return and 2 arguements
int compare(struct NODE *head,int n); //declaring compare function with return and 2 arguements
void display(struct NODE *); //declaring display function with no return and 1 arguement
#endif /* SETTHEORY_H_ */

project_1.c

#include"settheory.h" //header file for this project
int main() //main program of this project
{
struct NODE *head1=NULL;//head1 pointer of type struct NODE
struct NODE *head2=NULL;//head2 pointer of type struct NODE
struct NODE *res_intersection=NULL;//pointer to store values of intersection
struct NODE *res_union=NULL;//pointer to store values of union
int first_list_nodes=0,second_list_nodes=0,//variables to store vaues from user
first_list_items=0,second_list_items=0;
printf("\nEnter number of nodes for 1st list:");//asking size of 1st list
fflush(stdout);
scanf("%d",&first_list_nodes);//storing value
for(int i=0;i<first_list_nodes;i++)//for-loop to get data for nodes
{
printf("\nEnter the data %d of 1st list:",i+1);//asking for data
fflush(stdout);
scanf("%d",&first_list_items);//storing value
head1=insert_node(head1,first_list_items);
}
printf("\nData inside list 1 is:\t");//printing datas of 1st list given by user
display(head1);//calling display function to display values entered for 1st list
printf("\nEnter number of nodes for 2nd list:");//asking size of 2nd list
fflush(stdout);
scanf("%d",&second_list_nodes);//storing value
for(int i=0;i<second_list_nodes;i++)//for-loop to get data for nodes
{
printf("\nEnter the data %d of 2nd list:",i+1);//asking for data
fflush(stdout);
scanf("%d",&second_list_items);//storing value
head2=insert_node(head2,second_list_items);
}
printf("\nData inside list 2 is:\t");//printing datas of 1st list given by user
display(head2);//calling display function to display values entered for 2nd list
res_intersection=get_intersection(head1,head2);//calling get_intersection function to get common values from both list
printf("\nResult for Intersection :\t");
display(res_intersection);//calling display() to print result
res_union=get_union(head1,head2);//calling get_union function to get all unique values from both list
printf("\nResult for Union :\t");
display(res_union);//calling display() to print result
return 0;
}

get_union.c

// This code will take the head address of 2 lists to be compared for unique values.

//Then it will create new head pointer to store unique values from both the lists.

//After that it will iterate through the 2 lists for unique values and update it in the head pointer created.

//The address of the new pointer with unique values is returned.

#include"settheory.h"
struct NODE *get_union(struct NODE *head1,struct NODE *head2) //function definition
{
struct NODE *head_union=NULL;//new head pointer called head_union
while(head1!=NULL)//while loop to iterate through the 1st list
{
head_union=insert_node(head_union,head1->data);//inserting the list 1 values in head_union as they all are unique
head1=head1->next;//updating the next node address of list 1
}
while(head2!=NULL)//while loop to iterate through the 2nd list
{
if(!compare(head_union,head2->data))//using if logic and compare function to check whether any unique value is present
{
head_union=insert_node(head_union,head2->data);//if true then it will insert that value into get_union using insert_node function
}
head2=head2->next;//updating the next node address of list 2
}
return head_union;//returning the address of new list created
}
get_intersection.c
//This code will take the head address of 2 lists to be compared for common values,then it will create new head pointer to store common values.
//After that it will iterate through the 2 lists for common value and update it in the head pointer created.
//The address of the new pointer with common values is returned.
#include"settheory.h"
struct NODE *get_intersection(struct NODE *head1,struct NODE *head2) //function definition
{
struct NODE *head_intersection=NULL;//new head pointer called head_intersection
while(head1!=NULL)//while loop to iterate through the 1st list
{
if(compare(head2,head1->data))//using if logic to and compare function to check whether any common value is present
{
head_intersection=insert_node(head_intersection,head1->data);//if true then it will insert that data into get_intersection using insert_node function
}
head1=head1->next;//updating the next node address of list 1
}
return head_intersection;//returning the address of new list created
}
insert_node.c
//This code will take the address of head of list and value to be inserted.
//Then it will create new-node and store the value, update the address of head and then will return its new-node address as new reference address.
//It will add new-node at the beginning of the list.
#include"settheory.h"
struct NODE *insert_node(struct NODE *head,int data) //function definition for insert_node
{
printf("Inserting the data @ the beginning"); //printing that new_node is added in the beginning of linked list
struct NODE *new_node;//declaring a new_node pointer of struct NODE type
new_node=(struct NODE *)malloc(sizeof(struct NODE));//allocating space to new-node in memory using malloc
printf("\nThe address of new_node is:%p",new_node);//printing the memory address of new_node
new_node->data=data;//storing the data in new_node
printf("\nThe data inserted in the new_node is:%df\n",new_node->data);//printing the value of data to user
new_node->next=head;//assigning the address of head to new_node
head=new_node; //assigning the new_node address to head
return head; //returning the new reference address of list
}
compare.c
//This code will take the head address of a list and value to be compared to data present in list as second argument
//Then it will iterate through the list 1 value to check if match is obtained,if match if present it will return '1' or else it will return '0'.
#include"settheory.h"
int compare(struct NODE *c_node,int n) //function definition
{
while(c_node!=NULL)//using while loop to iterate through the list passed as argument
{
if(c_node->data==n)//if loop to check whether the value matches with the data passed as second argument
{
return 1;//if true then returns 1
}
c_node=c_node->next; //assigning the next node address
}
return 0; //if no value matches then returns 0
}
display.c
// This code will take the head address of a list to be displayed.
//It will iterate through the list and will print the data present in each node.
#include"settheory.h"
void display(struct NODE *n) //function definition
{
while(n!=NULL)//while loop to iterate through the list
{
printf("\t%d",n->data);//printing the data present in the list
n=n->next;//updating the next node address
}
printf("\n");
}

OUTPUT

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.