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.

Answer-

/*
 * Project1_Main.c
 *
 *  Created on: 26-Jul-2023
 *      Author: Lenovo
 */

#include "List.h"                                         /*Declare header file*/
int main()                                                /*Main Function*/
{
	int first_list_node=0, second_list_node=0, first_list_item=0, second_list_item=0; /*Here we declare integer data type with variable 'first_list_node','second_list_node', 'first_list_item','second_list_item'*/
	printf("Enter the number of nodes in first list:");   /*Here user enter the number of nodes required i.e.first list*/
	fflush(stdout);
	scanf("%d",&first_list_node);                         /*Here we read the first list node*/
	printf("Enter %d the elements:",first_list_node);     /*Here we enter the first list node*/
	for(int i=0;idata);                       /*Here we print the tail data*/
			tail=tail->next;                               /*Here we get the next next tail data till the end*/
		}
		tail=newnode;
	}
}

Node *insert_node(Node *head_ref,int n)                    /*Here we define the Node insert_node*/
{
	printf("\n Inserting elements at the beginning...\n");
	newnode=(Node*) malloc(sizeof(Node));                  /*Here we define the newnode and also give the size of node*/
	printf("Address of created newnode :%d\n",newnode);    /*Here we print the address of newnode*/
	newnode->data = n;                                     /*Here we get newnode till the n value*/
	printf("Inserted value is :%d\n",newnode->data);       /*Here we print the Inserted value */
	newnode->next =head_ref;
	head_ref=newnode;
	return head_ref;

}

Node *get_union(Node *head_pointer1,Node *head_pointer2)    /*Here we define the Node get_union*/
{
	while(head_pointer1 !=0)
	{
		head_union = insert_node(head_union,head_pointer1->data);
		head_pointer1=head_pointer1->next;
	}
	while(head_pointer2 !=0)
	{
		if(!compare(head_union,head_pointer2->data))
			head_union = insert_node(head_union,head_pointer2->data);
		head_pointer2 = head_pointer2->next;
	}
	return head_union;
}

int compare(Node *head,int data)                             /*Here we define the int compare*/
{
	while (head !=NULL)
	{
		if(head->data == data)
			return 1;
		head=head->next;
	}
	return 0;
}

Node *get_intersection(Node *head_pointer1,Node *head_pointer2)  /*Here we define the Node get_intersection*/
{
	while (head_pointer1 != 0)
	{
		if (compare(head_pointer2, head_pointer1->data))
			head_intersection = insert_node(head_intersection,head_pointer1->data);
		head_pointer1 = head_pointer1->next;
	}
	return head_intersection;
}


/*
 * List.h
 *
 *  Created on: 26-Jul-2023
 *      Author: Lenovo
 */

#ifndef LIST_H_
#define LIST_H_
#include
#include
#include

typedef struct node                            /*Here we used the typedef structure with variable node */
{
	int data;                                  /*Here we declare integer data type with variable 'data'*/
	struct node *next;
}Node;

static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection; /*Here we declare global variables*/

Node *insert_node(Node *head_ref,int n);                          /*Here we declare node insert node*/
void display_list(Node *head_ref);                                /*Here we declare display list*/
Node *get_union(Node *head_pointer1,Node *head_pointer2);         /*Here we declare node get union*/
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);  /*Here we declare node get intersection*/
int compare(Node *head,int data);                                 /*Here we declare int compare*/

#endif /* LIST_H_ */



Output-

Enter the number of nodes in first list:4
Enter 4 the elements:1 2 3 4

 Inserting elements at the beginning...
Address of created newnode :12395552
Inserted value is :1

 Inserting elements at the beginning...
Address of created newnode :12395584
Inserted value is :2

 Inserting elements at the beginning...
Address of created newnode :12415168
Inserted value is :3

 Inserting elements at the beginning...
Address of created newnode :12415200
Inserted value is :4
4321
 Enter the number of nodes in second list:4
Enter 4 the elements:4 5 6 7

 Inserting elements at the beginning...
Address of created newnode :12415232
Inserted value is :4

 Inserting elements at the beginning...
Address of created newnode :12415264
Inserted value is :5

 Inserting elements at the beginning...
Address of created newnode :12415296
Inserted value is :6

 Inserting elements at the beginning...
Address of created newnode :12415328
Inserted value is :7
7654
 Inserting elements at the beginning...
Address of created newnode :12415360
Inserted value is :4

 Inserting elements at the beginning...
Address of created newnode :12415392
Inserted value is :3

 Inserting elements at the beginning...
Address of created newnode :12415424
Inserted value is :2

 Inserting elements at the beginning...
Address of created newnode :12415456
Inserted value is :1

 Inserting elements at the beginning...
Address of created newnode :12415488
Inserted value is :7

 Inserting elements at the beginning...
Address of created newnode :12415520
Inserted value is :6

 Inserting elements at the beginning...
Address of created newnode :12415760
Inserted value is :5
Here is the result of union:5671234
 Inserting elements at the beginning...
Address of created newnode :12415856
Inserted value is :4
Here is the result of intersection: 4

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.