Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
LIVE BATCH STUDENT
OBJECTIVE:
The program is used perform the mathematical set theory functions such as intersection and union on the given sets.
FLOW OF PROGRAM:
- Get the number of elements in set from user
- Get the elements from user and store it as node in linked list
- Display the both the sets/linked list
- Create intersection of the two set
- Create union of the two set
- Display the intersection and union created
LOGIC:
INTERSECTION:
First element of set one is compared with each elements of set 2. If the any element of set 1 is equal to elements in set 2, the node is copied into the intersection set. For comparing each elements for() loop and if else condition are being used.
UNION:
The elements of set 1 is copied to union set. The each element of set 2 is compared with elements of set 1, if it is equal, then the element is ignored in the union set. for() loops and if else conditions are being used.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
//node is declared using struct
typedef struct node
{
int data;
struct node *nxt;
}node;
//to create set as linked list
node* insert(node *head, int n)
{
int temp_data;
for(int i=0;i<n;i++)
{
//Getting data of each node from user
//assigning next pointer to next node
printf("\nEnter the data for node%d :",i+1);
fflush(stdout);
scanf("%d",&temp_data);
(head+i)->data=temp_data;
if(i==n-1)//if it is last node,assign NULL to next
(head+i)->nxt=NULL;
else
(head+i)->nxt=head+i+1;
}
return head;
}
//function to display all nodes in the linked list
void display(node *p)
{
//print nodes till the next pointer is NULL
//next pointer is NULL for last node of the linked list
while(p!=NULL)
{
printf(" %d ",p->data);
fflush(stdout);
p=p->nxt;
}
}
//function to display common elements available in both sets
node* intersection(node *head1, int n1, node *head2, int n2)
{
node *result;
result = (node*)calloc((n1>=n2)?n2:n1,sizeof(node));
int k=0;
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
//compares the data of set 1 with all elements of set 2
if((head1+i)->data == (head2+j)->data)
{
(result+k)->data = (head1+i)->data;
(result+k)->nxt = result+k+1;
k++;
}
}
}
(result+k-1)->nxt = NULL;
result = (node*)realloc(result,k);
return result;
}
//function to display set with data of set 1 and set 2 ignoring the repeating data
node* union_set(node *head1, int n1, node *head2, int n2)
{
node *result1;
result1 = (node*)calloc(n1+n2,sizeof(node));
int k=n1;
for(int i=0;i<n1;i++)
{
//coping set 1 data into union set
(result1+i)->data = (head1+i)->data;
(result1+i)->nxt = result1+i+1;
}
//comparing set 2 data with set 1 to eliminate the repeating data
for(int i=0;i<n2;i++)
{
int flag=0;
for(int j=0;j<n1;j++)
{
if((head2+i)->data == (head1+j)->data)
continue;
else
flag++;
}
if(flag==n1)
{
(result1+k)->data = (head2+i)->data;
(result1+k)->nxt = result1+k+1;
k++;
}
}
(result1+k-1)->nxt = NULL;
return result1;
}
int main()
{
//head pointer and variable n are declared
node *head[2];
int n[2];
//creating set as linked list
for (int i=0;i<2;i++)
{
//getting number of nodes from user
printf("\nEnter number of nodes in %d set ",i+1);
fflush(stdout);
scanf("%d",&n[i]);
//Allocate memory dynamically using calloc
head[i] = (node*)calloc(n[i],sizeof(node));
head[i] = insert(head[i],n[i]);
}
//displaying the elements of the set
for (int i=0;i<2;i++)
{
printf("\nThe elements of set %d are ",i+1);
fflush(stdout);
display(head[i]);
}
//creating intersection set using function
node *res_intersection;
res_intersection = intersection(head[0], n[0], head[1], n[1]);
//displaying the elements of the intersection set
printf("\nThe elements of intersection set are");
fflush(stdout);
display(res_intersection);
//creating union set using function
node *res_union;
res_union = (node*)calloc(n[0]+n[1],sizeof(node));
res_union = union_set(head[0], n[0], head[1], n[1]);
//displaying the elements of the union set
printf("\nThe elements of union set are");
fflush(stdout);
display(res_union);
}
OUTPUT:
