Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

/*
* project1.c
*
* Created on: Jan 28, 2023
* Author: RINSHID PK
*/
#include<stdio.h>
#include<stdlib.h>

int union_set(int size,int a[]);
//creating first node(set A)
typedef struct{
int value;
struct set_A* next;
}set_A;

//creating second node(set B)
typedef struct{
int value;
struct set_B* next;
}set_B;

int union_set(int size,int a[])

{
int i,j,k;
for(i=0;i<size;i++)

{
for(j=i+1;j<size;)

{
if(a[i]==a[j])

{
for(k=j;k<size;k++)

{
a[k]=a[k+1];
}
size--;
}

else

{
j++;
}
}
}
return(size);
}//Union function: removing same elements in the dummy_array and return new size of the dummy_array


int main()
{
int d=0,size;//variable declaration
int setA_elmnts_count =0;
int setB_elmnts_count =0;
printf("Enter the elements count of set A : ");//set A elements count
fflush(stdout);
scanf("%d",&setA_elmnts_count);
printf("Enter the elements count of set B : ");//set B elements count
fflush(stdout);
scanf("%d",&setB_elmnts_count);
size = setA_elmnts_count+setB_elmnts_count;//dummy array size = size of setA +setB
//initializing set_A and Set_B
set_A * head_A;
set_B * head_B;
set_A * elements_A[setA_elmnts_count];
set_B * elements_B[setB_elmnts_count];
int dummy_array[size] ;//dummy array for finding union
//set A
for(int i=0;i<setA_elmnts_count;i++)
{
elements_A[i] = NULL;
elements_A[i] = malloc(sizeof(set_A));
}//allocate memory for set_A
for(int i=0;i<setA_elmnts_count;i++)
{
printf("enter the elements of set A : ");
fflush(stdout);
scanf("%d",&elements_A[i]->value);//assign value to set_A from console
elements_A[i]->next = elements_A[i + 1];//connecting nodes
dummy_array[i] = elements_A[i]->value;//assigning set_A values to dummy_array

d=i;
}//assign values to set_A
elements_A[d]->next = NULL;//assign last node of set_A is NULL


//set B
for(int j=0;j<setB_elmnts_count;j++)
{
elements_B[j] = NULL;
elements_B[j] = malloc(sizeof(set_B));
}//allocate memory for set_B

for(int j=0;j<setB_elmnts_count;j++)
{
printf("enter the elements of set B : ");
fflush(stdout);
scanf("%d",&elements_B[j]->value);//assign value to set_B from console
dummy_array[setA_elmnts_count + j] = elements_B[j]->value;//assigning set_B values to dummy_array
elements_B[j]->next = elements_B[j + 1];//connecting nodes
d=j;
}//assign values to set_B
elements_B[d]->next = NULL;//assign last node of set_B is NULL
//printing set A
head_A=elements_A[0];//set head_A is the address of the first element of set_A
printf("\n set A elements are ...\n");
while(head_A != NULL)
{
printf("%d,",head_A->value);
head_A = head_A->next;
}
//printing set B
head_B=elements_B[0];//set head_A is the address of the first element of set_B
printf("\n set B elements are ...\n");
while(head_B != NULL)
{
printf("%d,",head_B->value);
head_B = head_B->next;
}
//intersection
head_A=elements_A[0];//set head_A is the address of the first element of set_A
head_B=elements_B[0];//set head_A is the address of the first element of set_B
printf("\n Intersection of set A and set B are ...\n");
while(head_A != NULL)
{
while(head_B != NULL)
{
if (head_A->value==head_B->value)
{
printf("%d,",head_A->value);
}
head_B = head_B->next;
}
head_B=elements_B[0];
head_A = head_A->next;
}
size = union_set(size,dummy_array);//calling union function
printf("\n Union of set A and set B are .....\n");

for(int i=0;i<size;i++)
{
printf(" %d",dummy_array[i]);
}//printing Union set
}

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.