Challenge

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

1.MATHEMATICAL SET THEORY
/*
* mathematical_set.c
*
* Created on: 16-Jul-2023
* Author: HP
*/
#include "set_theory.h"
int main()
{
struct NODE *head1 = NULL;
struct NODE *head2 = NULL;
struct NODE *res_intersection = NULL;
struct NODE *res_union = NULL;
int first_list_nodes = 0, second_list_nodes = 0, // Declaring variables for storing the values from user
first_list_items = 0, second_list_items = 0;
printf("Enter the number of Nodes for 1st List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
for(int i = 0; i<first_list_nodes; i++)
{
printf("\nEnter the data %d of 1st list: ",i+1);
fflush(stdout);
scanf("%d",&first_list_items);
// Calling the insert_node function to insert the value enter by user, in new-node and getting the address of new node in return
head1 = insert_node(head1,first_list_items);
}
printf("\nThe data inside list 1 is:\t");
display(head1);
printf("\nEnter the number of Nodes for 2nd List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
for(int i = 0; i<second_list_nodes; i++)
{
printf("\nEnter the data %d of 2nd list: ",i+1);
fflush(stdout);
scanf("%d",&second_list_items);
// Calling the insert_node function to insert the value enter by user in new-node and getting the address of new node in return
head2 = insert_node(head2,second_list_items);
}
printf("\nThe data inside list 2 is:\t");
display(head2);
//Calling the get_intersection function to get the common values in both lists and passing head1 and head1 as arguments
res_intersection = get_intersection(head1,head2);
printf("\nThe result of intersection is:\t");
display(res_intersection);
//Calling the get_union function to get the all unique values from both lists and passing head1 and head1 as arguments
res_union = get_union(head1,head2);
printf("\nThe result of union is:\t");
display(res_union);
return 0;
}
//insert_node
struct NODE *insert_node(struct NODE *head,int data) // Definition of insert_node function
{
printf("\nInserting the data at the beginning"); // Printing that new-node is added in the beginning of linked list
struct NODE *newnode; // Declaring a new-node pointer of struct NODE type
newnode = (struct NODE *)malloc(sizeof(struct NODE)); // Allocating space to new-node in memory using malloc
printf("\nThe address of the newnode is: %p",newnode); // Printing the memory address of new-node
newnode->data = data; // Storing the data in new-node entered by user
printf("\nThe data inserted in the newnode is: %d\n",newnode->data); // Printing the value of data to user
newnode->next = head; // Assigning the address of head to new-node
head = newnode; // Assigning the new-node address to head
return head;
}
//compare
int compare(struct NODE *c_node,int n) // Definition of display function
{
while(c_node != NULL)
{
// Using if loop to check whether the value matches with the data passed as second argument
if(c_node->data == n)
{
return 1;
}
c_node = c_node->next;
}
return 0;
}
//get_intersection
struct NODE *get_intersection(struct NODE *head1,struct NODE *head2) // Definition of get_interestion function
{
struct NODE *head_intersection = NULL;
while(head1 != NULL)
{
// Using if logic to and compare function to check whether any common value is present
if(compare(head2,head1->data))
{
// If true then it will insert that data into get_intersection using insert_node function
head_intersection = insert_node(head_intersection,head1->data);
}
head1 = head1->next;
}
return head_intersection;
}
// get_union
struct NODE *get_union(struct NODE *head1, struct NODE *head2) // Definition of get_union function
{
struct NODE *head_union = NULL;
while(head1 != NULL)
{
// Inserting the list 1 values in head_union as they all are unique
head_union = insert_node(head_union,head1->data);
head1 = head1->next;
}
while(head2 != NULL)
{
// Using if logic and compare function to check whether any unique value is present
if(!compare(head_union,head2->data))
{
// If true then it will insert that value into get_union using insert_node function
head_union = insert_node(head_union,head2->data);
}
head2 = head2->next;
}
return head_union;
}
// display_list
void display(struct NODE *n) // Definition of display function
{
while (n != NULL) // Using 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 of the list
}
printf("\n"); // After printing entering the new line in console
}

2. SET THEORY HEADER FILE:

/*
* set_theroy.h
*
* Created on: 16-Jul-2023
* Author: HP
*/
#ifndef SET_THEORY_H_
#define SET_THEORY_H_
#include<stdio.h>
#include<stdlib.h>
struct NODE // Creating the node for linked list using structure
{
int data;
struct NODE *next;
};
struct NODE *insert_node(struct NODE *,int ); // Declaring the insert_node function with return and 2 arguments
struct NODE *get_union(struct NODE *, struct NODE *); // Declaring get_union function with return and 2 arguments
struct NODE *get_intersection(struct NODE *,struct NODE *); // Declaring get_intersection function with return and 2 arguments
int compare(struct NODE *head,int n); // Declaring compare function with return and 2 arguments
void display(struct NODE *); // Declaring display function with no return and one argument
#endif /* SET_THEORY_H_ */

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.