Challenge

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: (5 to 6 lines)
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists

Aim:

Goals (2 to 3 points)
Implementation of interface for mathematical Set Theory using C programming.
Giving two list of data as input to get the output as union & intersection for given list of data.

Key Highlights of the Project: (4 to 5 point)
Creating two list of data using linked list method.
Insert the node to 1st and 2nd list accordingly.
Intersection of data of two lists.
Getting Union of two list.

Deliverables of this project: (4 to 5 points)
Intersection of data of two list.
Union of data of two list

Expected Outcomes: (4 to 5 points)
Student will get the Knowledge of creating the nodes using Linked List.
Getting the intersection and Union of data of two list.
Comparing the elements of two list.

Solution:

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 crerated as a linked lisr. so here we are creating a structure it has a collecion different dara. Here is the process to first creare two lisrs using the reference address. The main concept of linkedlist is self referential structure.

Implementation Of C interface for mathematical set theory using C progamming:

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

Creating two lists of data using the linked list method:

Firsr, 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 heas address and value from the user.Inside the inserted node, we are creating the size of the lisr using the malloc funciton. 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 head 2. 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 funcgtion as same as the function which is used for the creation of the inked lisr. Using another while loo[ 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 unionn in the list.

Getting Intersection 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.

printed function:

It is used to print the data of teh different lists by using different arguments passing to the printed function. So creating the temaparary 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 has last node.

Main file:

#include"list.h"//declaring the header file
//declaring the structure variables.

int main() //starting the main function.
{
int i, set1Count,set2Count, temp;//declaring the variables in the main function.
//Creating First linked List.
printf("Enter number of nodes in first Linked List\n");//printing the statement to enter the value of number of nodes.
fflush(stdout);
scanf("%d", &set1Count);//loading the value.
printf("Enter %d integers\n", set1Count);//print statement to enter the given number of values.
fflush(stdout);
for(i=0; i<set1Count; i++)//for loop operation
{
scanf("%d", &temp);//loading the values in temp.
insert(&set1, temp);//sending the values of temp into set1 address.
}
printLinkedList(set1);//printing the set1 values.
fflush(stdout);
printf("\nEnter number of nodes in second Linked List\n");//printing the statement to enter the value of number of nodes.
fflush(stdout);
scanf("%d", &set2Count);//loading the value.
printf("Enter %d integers\n", set2Count);//print statement to enter the given number of values.
fflush(stdout);
for(i=0; i<set2Count; i++)//for loop operation
{
scanf("%d", &temp);//loading the values in temp.
insert(&set2, temp);//sending the values of temp into set2 address.
}
printLinkedList(set2);//printing the set2 values.
/* printing Union of two given linked list */
unionfun(set1, set2);//getting the values of union function from .c file.
//getting the values of intersection function from .c file.
printf("\nUnion Linked List\n");//printing statement.
printLinkedList(unionvalues);//printing the values.
fflush(stdout);
intersection(set1,set2);
printf("\nIntersection Linked List\n");//printing statement.
printLinkedList(intersectionvalues);//printing the values
fflush(stdout);
return 0;
}
void printLinkedList(struct node *set)//function for printing the values of sets.
{
while (set != NULL) //node values are not equal to NULL then it goes.
{
printf("%d", set->data);//printing thr first value.
set = set->next;//going for the next data.
if(set != NULL)//If the value of node is equal to NULL then the process will stop. The last value of node is NULL.
printf("-->");
}
}

Source file:

/*
* sorce.c
*
* Created on: 03-Aug-2022
* Author: IND
*/
#include "list.h"//librery file
//declaring the structure variables.

void insert(struct node **head, int n)//creating the n number of nodes.
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));//node creating syntax.
printf("\n.....inserted element....\n");
printf("addres of created node:%d\n",newNode);
fflush(stdout);
newNode->data = n;//sending the data to the new node.
printf("inserted value %d\n",newNode->data);
fflush(stdout);
newNode->next = *head;//.
*head = newNode;//.
}
struct node* unionfun(struct node *set1, struct node *set2)//union function declaration.
{
//Add all nodes of first Linked List to unionvalues
struct node *temp = set1;//giving structure as temp is equal to set1.
while(temp != NULL)//if the temp is not equal to NULL then the process will continue.
{
insert(&unionvalues, temp->data); //loading the temp values one by one to union values.
temp = temp->next; //going foy the next value.
}
// Insert those nodes of set2 which is not present in set1
while(set2 != NULL)
{
if(!compare(set1,set2->data)) //with this condition the unequal values in the set2 will go.Else it goes for the next value.
{
insert(&unionvalues, set2->data); //that unequal value will go to union values.
}
set2 = set2->next; //to go for the next value compare.
}
return unionvalues; //giving the union values to the main function.
}

//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *set1, struct node *set2)//structure for printing the intersection values.
{
intersectionvalues = NULL;
//Search every element of set1 in set2,If found then add it to intersection List.
while(set1 != NULL)//going to the while loop.
{
if(compare(set2, set1->data))//comparing the set2 and set1 values with holding the set2 value as constant.
{
insert(&intersectionvalues, set1->data);//if the value of set 1 is equal to the value of set2 then it goes to intersection .
}
set1 = set1->next;//going for the next value comparing.
}
return intersectionvalues;//giving all values to the main function.
}
int compare(struct node *head, int n)//compare function which is used by the union and intersection.
{
while (head != NULL)//check condition.if satisfied then go.
{
if (head->data == n)//if data in head is equal to the n value then it returns the value in n. Else goes for the next value.
{
return 1;
}
head = head->next;//next value.
}
return 0;
}
//Prints a linked list from head node till tail node.

Hedder file:

/*
* list.h
*
* Created on: 03-Aug-2022
* Author: IND
*/

#ifndef LIST_H_
#define LIST_H_
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
} *set1, *set2, *unionvalues, *intersectionvalues;


#endif /* LIST_H_ */

Out put:

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.