Challenge

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

/*
 ============================================================================
 Name        : Project_1.c
 Author      : Om Deore
 Date        : 08/08/2022
 Copyright   : Your copyright notice
 Description : Creation of user defined data type to implement the user interfaces
               for working with ‘Set’ (Mathematical Set theory) using Linked List
 ============================================================================
 */

#include "set_theory.h"                                 // Including the set_theory.h header file

int main()                                              // Main program
{
	struct NODE *head1 = NULL;                          // Creating head1 pointer of type struct NODE
	struct NODE *head2 = NULL;                          // Creating head2 pointer of type struct NODE
	struct NODE *res_intersection = NULL;               // Creating res_intersection pointer of type struct NODE
	struct NODE *res_union = NULL;                      // Creating res_union pointer of type struct NODE

	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:");  // Asking user for the size of 1st list
	fflush(stdout);                                     // Clearing output stream
	scanf("%d",&first_list_nodes);                      // Storing the value

	for(int i = 0; i<first_list_nodes; i++)             // Using for loop to get the data of each node
	{
		printf("\nEnter the data %d of 1st list: ",i+1);// Asking user to enter the data for particular node
		fflush(stdout);                                 // Clearing the output stream
		scanf("%d",&first_list_items);                  // Storing the value of particular node

		// 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");           // Printing the data entered by user in List 1
	display(head1);                                     // Calling the display function to print the list 1

	printf("\nEnter the number of Nodes for 2nd List:");// Asking user for the size of 2nd list
	fflush(stdout);                                     // Clearing output stream
	scanf("%d",&second_list_nodes);                     // Storing the value

	for(int i = 0; i<second_list_nodes; i++)            // Using for loop to get the data of each node
	{
		printf("\nEnter the data %d of 2nd list: ",i+1);// Asking user to enter the data for particular node
		fflush(stdout);                                 // Clearing the output stream
		scanf("%d",&second_list_items);                 // Storing the value of particular node

		// 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");           // Printing the data entered by user in List 2
	display(head2);                                     // Calling the display function to print the list 2

	//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);   // Updating the address of result list in res_intersection
	printf("\nThe result of intersection is:\t");       // Printing the common values from both list
	display(res_intersection);                          // Calling display function to print the result

	//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);                 // Updating the address of result list in res_union
	printf("\nThe result of union is:\t");              // Printing the all unique values from both list
	display(res_union);                                 // Calling display function to print the result

	return 0;
}
/*
 * set_theory.h
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: This is the header file and will include all the libraries required,
 *              also declaration of functions, structures
 */

#ifndef SET_THEORY_H_                // Un-defining the name if already defined
#define SET_THEORY_H_                // Defining the name again for library

#include<stdio.h>                    // Pre-processor directives
#include<stdlib.h>                   // Adding the stdio and stdlib library to use necessary functions

struct NODE                          // Creating the node for linked list using structure
{
	int data;                        // 1st member is the data for node
	struct NODE *next;               // 2nd member to store address of next node
};

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_ */            // End of library
/*
 * insert_node.c
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: 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 "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("\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;                                      // Returning the new reference address of list
}
/*
 * compare.c
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: 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 "set_theory.h"                 // Including the set_theory.h header file

int compare(struct NODE *c_node,int n)  // Definition of display function
{
    while(c_node != NULL)               // Using while loop to iterate through the list passed as argument
    {
    	// Using if loop to check whether the value matches with the data passed as second argument
       if(c_node->data == n)
       {
    	   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
}
/*
 * get_intersection.c
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: 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 "set_theory.h"                                               // Including the set_theory.h header file

struct NODE *get_intersection(struct NODE *head1,struct NODE *head2)  // Definition of get_interestion function
{
	struct NODE *head_intersection = NULL;                            // Creating new head pointer called head_intersection

	while(head1 != NULL)                                              // Using while loop to iterate through the 1st list
	{
		// 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;                                          // Updating the next node address of list 1
	}
    return head_intersection;                                         // Returning the address of new list created
}
/*
 * get_union.c
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: 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 "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
{
	struct NODE *head_union = NULL;                            // Creating new head pointer called head_union

	while(head1 != NULL)                                       // Using while loop to iterate through the 1st list
	{
		// Inserting the list 1 values in head_union as they all are unique
		head_union = insert_node(head_union,head1->data);
		head1 = head1->next;                                   // Updating the next node address of list 1
	}

	while(head2 != NULL)                                       // Using while loop to iterate through the 2nd list
	{
		// 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;                                   // Updating the next node address of list 2
	}
	return head_union;                                         // Returning the address of new list created
}
/*
 * display_list.c
 *
 *  Created on: Aug 8, 2022
 *      Author: OM
 * Description: 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 "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 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
}

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.