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 :

Overview of the Project :

Here the main objective of the project is to do the mathematical set operation. The set operation has many operations here they are told to do the union and intersection of the list created as a linked list. so here we are creating a structure it has a collection of different data. Here is the process to first create two lists using the reference address. The main concept of LinkedList is self-referential structure.

Implementation of interface for mathematical Set Theory using C programming :

First, we are creating the structure as a typedef model to rename the user-defined name. In that, we are creating two data types int for storing the integer value, and the pointer variable is used it storing the address of the same structure itself. After creating the main function we get the values of list 1 and list 2 using the for loop and using the many function call in that and then we are creating the different functions which are mentioned in the main program.

Creating two lists of data using the linked list method :

First, we are getting the size of the list. Using the for loop we are inserting the data. Inside the foe loop, we use the insert node function. In these functions, we are using arguments are head address and value from the user. Inside the inserted node, we are creating the size of the list using the malloc function. Entering the data in the data area, And storing the address of the head to the next pointer and we are storing the new node address in the head for the next update. Likewise, we are creating the two lists.

Getting Union of two lists:

Here we get a function call of getting union with the argument of head1 and head2. In the union function, we are using the head unit as a list by inserting the value of the first list in the union and jumping to the next area by addressing stores in the next. Insert node function as same as the function which is used for the creation of the linked list. Using another while loop we are getting the second list. Using the compare function using not we are getting only the value that is not placed in the list one and inserted in the union in the list.

Getting Intersectionthe of two lists:

In the intersection, we are easy to get an intersection because just we compare to list and return the value which is placed in both lists commonly. Using the If command comparing the data of lists 1 and 2. If the condition is true it passes the next step which has an intersection list and using the insert function, we are inserting the data into that list.

Display function:

It is used to display the data of the different lists by using different arguments passing to the display function. So creating the temporary list with the head is null. And print data and use the next address stored in the pointer variable pointing to the structure. If head next has it recognize it is last node.

PROGRAM :

/* Creation of user defined data type to implement the Mathematical Set theory using Linked List */
#include<stdio.h> // Standard input-output pre-processor directive

#include<stdlib.h> // Pre-processor directive for standard library functions

typedef struct node // Defining a structure
{
	int data;
	struct node *next; // Self-referrential node holding an address
}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*result_union,*result_intersection; // Declaring the different variables in structure type

//Declaring different function with the arguments
Node *insert_node(Node *head_ref,int n);
void display_list(Node *head_ref);
Node *get_union(Node *head_pointer1,Node *head_pointer2);
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);
int compare(Node *head,int data);

void display_list(Node *head_ref) // Expanding the function to display the nodes
{
	if(head_ref == 0)
	{
		printf("List is empty");
	}

	else
	{
		tail = head_ref; // Transferring the address of the head reference to the tail
    while(tail != 0)
    {
    	printf("%d ", tail->data); // Prints the data of current node
    	tail = tail->next; // Advances the position of the current node to the next node
    }
    tail = newnode;
	}
}

Node *insert_node(Node *head_ref, int n) // Function to create a new list
{
	printf("\nInserting Elements at the beginning....\n");
	newnode = (Node*) malloc(sizeof(Node)); // Allocating a single memory block to the nodes
	printf("Address of created newnode : %p\n",newnode);
	newnode -> data = n; // Assigning the value to the node
	printf("Inserted value is : %d\n",newnode->data);
	newnode -> next = head_ref; // Address of the previous node is transferred to the new node
	head_ref = newnode; // Transferring the address of the new node to the head reference for linking a new node

	return head_ref;
}

Node *get_union(Node *head_pointer1,Node *head_pointer2) // Expanding the function used to perform the union operation
{
	while(head_pointer1 != 0)
	{
		if (!compare(head_union, head_pointer1->data)) // Inserting the items in list-1 once in the union, which are common in both the lists
		head_union = insert_node(head_union,head_pointer1->data); // Inserting the data from list-1 into the union function
		head_pointer1 = head_pointer1 -> next; // Point to the next node by the address of the next node
	}

	while (head_pointer2 != 0)
	{
		if (!compare(head_union, head_pointer2 -> data)) // Inserting the items in list-2 once in the union, which are common in both the lists
		head_union = insert_node(head_union,head_pointer2 -> data); // Inserting the data from list-2 into the union function
		head_pointer2 = head_pointer2 -> next; // Point to the next node by the address of the next node
    }
	return head_union;
}

//function to compare the node data with a value by linearly traversing across head to tail in a linked list
int compare(Node *head, int data)
{
	while (head != 0)
	{
		if (head -> data == data) // Comparing the data of list 2 with list 1
			return 1;
		head = head -> next;
	}
	return 0;
}

Node *get_intersection(Node *head_pointer1,Node *head_pointer2) // Function to get the intersection of two linked lists
{
	while (head_pointer1 != 0)
	{
		if (compare(head_pointer2, head_pointer1 -> data)) // Comparing the both list for intersection function
		head_intersection = insert_node(head_intersection,head_pointer1 -> data); // Transferring head pointer values to the intersection function
		head_pointer1 = head_pointer1 -> next;
	}
	return head_intersection;
}

int main()
{

	int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,second_list_items = 0;
	int f;

	printf("Enter the number of nodes in first linked list:");
	fflush(stdout);
	scanf("%d", &first_list_nodes);

	printf("Enter the values for %d nodes:", first_list_nodes); // Getting the number of elements in the first list
	for (f=0;f<first_list_nodes;f++)
	{
		fflush(stdout);
		scanf("%d", &first_list_items); // Getting the data in the run time
		head1 = insert_node(head1,first_list_items); // Inserting the data into the first list by calling the function
	}
	display_list(head1); // Calling the function to display the first list

	printf("\n\nEnter the number of nodes in second linked list : ");
	fflush(stdout);
	scanf("%d", &second_list_nodes);

	printf("Enter the values for %d nodes : ", second_list_nodes); // Getting the number of elements in the second list
	for (f=0;f<second_list_nodes;f++)
	{
		fflush(stdout);
		scanf("%d", &second_list_items); // Getting the data in the run time
		head2 = insert_node(head2,second_list_items); // Inserting the data into the second list by calling the function
	}
	display_list(head2); // Calling the function to display the second list

	result_union = get_union(head1,head2); // Transferring the result of the union function
	printf("\nUnion of the two linked lists : ");
	display_list(result_union); // Displaying the result of union

	result_intersection = get_intersection(head1,head2); // Transferring the result of the intersection function
	printf("\nIntersection of the two linked lists : ");
	display_list(result_intersection); // Displaying the result of intersection

}

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.