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 Overview:

In this project, we can learn about mathematical set theory using a C program with the help of the linked list. For this, we must create/insert 2 lists, compare each other, and intersect and union the list element in the list.

#include<stdio.h> //header file
#include<stdlib.h>
#include<math.h>

typedef struct node //defining structure
{
	int data; //creating data type of the variable
	struct node *next; //creating self referential node
}Node;

static Node *head1, *head2, *head_union, *head_intersection, *newnode, *tail, *res_union, *res_intersection; //declaring different function with argument

Node *insert_node(Node *head_ref,int n); //sub functions
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) //function used to display the node data
{
	if(head_ref==0)
	{
		printf("List is empty");
	}
	else
	{
		tail = head_ref; //we are transferring address of the list to tail
		while(tail !=0)
		{
			printf("%d", tail->data); //print the data of the current node
			tail = tail->next; //advance the position of the current node
		}
		tail = newnode;
	}
}

Node *insert_node(Node *head_ref,int n) //function used to create new list
{
	printf("\n Inserting Elements at the beginning... \n");
	newnode =(Node*) malloc(sizeof(Node)); //allocating memory to the nodes
	printf("Address of created newnode: %d\n",newnode);
	newnode->data = n; //assigning value to the node
	printf("Inserted value is: %d\n",newnode->data);
	newnode->next = head_ref; //address of the previous address is transferred to the new node
	head_ref = newnode; //transferring new address to the head reference
	return head_ref;
}

Node *get_union(Node *head_pointer1,Node *head_pointer2) //function used to union to list
{
	while(head_pointer1 !=0)
	{
		head_union = insert_node(head_union,head_pointer1->data); //here we are inserting the 1 list data into the union function
		head_pointer1 = head_pointer1->next; //we transverse the node using the address in next
	}
	while(head_pointer2 !=0)
	{
		if(!compare(head_union,head_pointer2->data)) //inserting the element not in list 1
			head_union = insert_node(head_union,head_pointer2->data); //here we are inserting the 2 list data into the union function
		head_pointer2 = head_pointer2->next; //travel to next node
	}
	return head_union;
}
int compare(Node *head,int data) //function used to compare
{
	while (head !=NULL)
	{
		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 used for intersection
{
	while (head_pointer1 !=0)
	{
		if (compare(head_pointer2, head_pointer1->data)) //we are comparing both the list for intersection
			head_intersection = insert_node(head_intersection,head_pointer1->data); //transferring value to the intersection
		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 List: ");
	fflush(stdout);
	scanf("%d",&first_list_nodes);
	printf("Enter %d the elements: ",first_list_nodes); //getting the number of element in the list
	for( f = 0;f < first_list_nodes;f++)
	{
		fflush(stdout);
		scanf("%d",&first_list_items); //getting the data in runtime
		head1 = insert_node(head1,first_list_items); //inserting data in the list
	}
	display_list(head1); //displaying the head list
	printf("\nEnter the number of Nodes in second list: ");
	fflush(stdout);
	scanf("%d",&second_list_nodes);
	printf("Enter %d the elements: ",second_list_nodes);
	for(f = 0;f< second_list_nodes;f++)
	{
		fflush(stdout);
		scanf("%d",&second_list_items); //getting the data in runtime
		head2 = insert_node(head2,second_list_items);
	}
	display_list(head2); //displaying head list 2
	res_union = get_union(head1,head2); //transferring the result of union function to the res_union
	printf("Here is the result of union: ");
	display_list(res_union); //transferring values of arguments to the display function
	res_intersection = get_intersection(head1,head2); //transferring the value of intersecting function to the res_intersection

	printf("Here is the result of intersection: ");
	display_list(res_intersection);
}
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.