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:

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

Goals :

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

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

  • Intersection of data of two list.
  • Union of data of two list

Expected Outcomes:

  • 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.

SET THEORY:
Set theory is the mathematical theory of well-determined collections, called sets, of objects that are called members, or elements, of the set. Pure set theory deals exclusively with sets, so the only sets under consideration are those whose members are also sets

UNION : in union we will consider all the elements.
INTERSECTION : in intersection only the common elements will be considered.


INTERSECTION UNION

PROGRAM FLOW:
1. To get inputs from user.
2. Generating the linkedlist for that data.
3. usage the display function
4. categories into union & intersection for results.

Explanation:

1.header section :
in the header section we have included the preprocessor derivatives like stdio.h, etc and as were using addition source and header file so the extension of the header file is included in thats section as set.h. and the name of additional source file and header file are fixed as common name.





2. main file :
in the main function using scanf the input for two lists was get from the user, and the size of list also calculated by getting it from user by assigning a variable node and the data was stored initially in the variable elements. After collecting the data it was printed once to check it before starting the set theory process. once is was verified it was connected with insert() and display() functions.


3. insert, display :
In the insert section the size of node was calculated by mentioning sizeof(). After this process the elements was alinged to is form. in linked list every node will have a data block and address block, the elements received from the used was attached in the data section and the address of the respective nodes will be attahed in this section. In display section thhe prresence of node will be vverified and it will priint the data.


4. union, intersection:
For union and intersection we have created a seperate function and using the compare function the elements present in list1 & list 2 will be printed as per union or intersection.



INPUT :

MAIN.C

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include"set.h"

int main()
{
int i=0,node1=0, element1=0,node2=0, element2=0,head1=0,head2=0; // assigning variables
printf("enter the required elements in list1"); // list1
fflush(stdout);
scanf("%d", &node1); // elements count
printf("enter the numbers");
fflush(stdout);
for(i=0; i<node1; i++) // loop
{
fflush(stdout);
scanf("%d", &element1); // data
fflush(stdout);
printf("\n enter the numbers: %d",element1);
head1=insert(head1,element1); // equating function insert with head1.
}
display(head1); // display head1 data
printf("enter the required elements");
fflush(stdout);
scanf("%d", &node2);
printf("enter the numbers");
fflush(stdout);
for(i=0; i<node2; i++)
{
fflush(stdout);
scanf("%d", &element2);
fflush(stdout);
printf("\n enter the numbers: %d",element2);
head2=insert(head2,element2);
}
display(head2);

res_union=get_union(head1,head2); // we define the res_union
printf("Here is the result of union:"); //Here we get result of union
display(res_union);
res_intersection=get_intersection(head1,head2); //Here we define the res_intersection
printf("Here is the result of intersection: "); //Here we get result of intersection
display(res_intersection);

}


SET.H

#ifndef SET_H_
#define SET_H_
#include <stdio.h>
#include<stdbool.h>
#include <stdlib.h>

typedef struct node //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;

Node *insert_node(Node *head_pointing,int b); /*Here we declare node insert node*/
void display(Node *head_pointing); /*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);


#endif /* SET_H_ */


SET.C:

#include"set.h"


Node *insert(Node *head_pointing,int b)
{
printf("\n Inserting elements at the beginning...\n");
newnode=(Node*) malloc(sizeof(Node)); // size of node
printf("\n Address of created newnode :%d",newnode);
newnode->data = b;
printf("Inserted value is :%d\n",newnode->data);
newnode->next =head_pointing;
head_pointing=newnode;
return head_pointing;
}

void display(Node *head_pointing)
{
if(head_pointing==0) //we check the head_pointing is equal to zero
{
printf("List is empty");
}
else
{
tail=head_pointing;
while(tail !=0)
{
printf("%d",tail->data);
tail=tail->next;
}
tail=newnode;
}
}

Node *get_union(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1 !=0)
{
head_union = insert(head_union,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
while(head_pointer2 !=0)
{
if(!compare(head_union,head_pointer2->data))
head_union = insert(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next;
}
return head_union;
}

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

Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while (head_pointer1 != 0)
{
if (compare(head_pointer2, head_pointer1->data))
head_intersection = insert(head_intersection,head_pointer1->data);
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}



OUTPUT :










RESULT :
Mathematical Set Theory was implemented successfully using C programming language and results was generated for union and intersection using linked list.




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.