Challenge

Project 1 - 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

#ifndef SET_THEORY_H_
#define SET_THEORY_H_

#include                    // Pre-processor directives
#include                   // 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 
#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
}
#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
}
#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
}
#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
}
#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.