Challenge

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

1. Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)

sol:

//declaring standard libraries

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

//declaring structures and universal names

struct Node{

int data;

struct Node *next;

}*array1,*array2,*tmp,*getUnions,*getIntersections;

void getUnion(struct Node *array1,struct Node *array2);

void insertArray(struct Node *array,int n);

void display(struct Node *array);

void push(struct Node *array,int data,int flag);

bool isPresent(struct Node *array,int data );

void getIntersection(struct Node *head1,struct Node *head2);

void insertArray(struct Node *array,int n){

struct Node *fnode,*temp;

int num;

if(array==NULL){//if address is equal to null then the memory is not allocated

printf("memory not allocated");

}

else{

//need to enter the values to insert into list

printf("enter value 1:");

scanf("%d",&num);

array->data=num;

array->next=NULL;

temp=array;

for(int i=2;i<=n;i++){

printf("enter the value %d : ",i);

scanf("%d",&num);

fnode=malloc(sizeof(struct Node));

fnode->data=num;

fnode->next=NULL;

temp->next=fnode;

temp=temp->next;

}

}

}

void display(struct Node *array){

struct Node *temp;

if(array==NULL){//if address is equal to null then the memory is not allocated or list is empty

printf("the list is empty");

}

else{

//each of the list is printed here until the address becomes null

temp=array;

while(temp!=NULL){

printf("%d \t",temp->data);

temp=temp->next;

}

printf("\n");

}

}

void push(struct Node *array,int data,int flag){

struct Node *fnode;

//first element is inserted into array

if(flag==0){

array->data=data;

array->next=NULL;

tmp=array;

}

else{// and remaining values are stored using creating fnode

fnode=malloc(sizeof(struct Node));

fnode->data=data;

fnode->next=NULL;

tmp->next=fnode;

tmp=tmp->next;

}

}

bool isPresent(struct Node *array,int data ){

struct Node *duplicate;

duplicate=array;

while(duplicate!=NULL){//if data is present in list 2 then it return true

//loop continues till list points to null address

if(data==duplicate->data){

return true;

}//and variable points to next address

duplicate=duplicate->next;

}

return false;

}

void getUnion(struct Node *head1,struct Node *head2){

struct Node *t1,*t2;

int flag=0;

t1=head1;

t2=head2;

getUnions=malloc(sizeof(struct Node));//to store union list memory is allocated

if(t1==NULL){//loop continues till list 1 points to null address

printf("the list is empty");

}

else{

while(t1!=NULL){//list 1 data is pushed into union list

push(getUnions,t1->data,flag);

flag++;

t1=t1->next;

}

if(t2==NULL){

printf("the list is empty");

}

else{

while(t2!=NULL){//loop continues till list 2 points to null address

if(!isPresent(getUnions,t2->data)){//if list2 is not present in union list then only it is pushed into union list

push(getUnions,t2->data,flag);

}

t2=t2->next;

}

}

}

}

void getIntersection(struct Node *head1,struct Node *head2){

struct Node *t1,*t2;

int flag=0;

t1=head1;

t2=head2;

getIntersections=malloc(sizeof(struct Node));//to store intersection list memory is allocated

if(t1==NULL){

printf("the list is empty");

}

else{

while(t1!=NULL){//loop continues till list 1 points to null address

if(isPresent(t2,t1->data)){//if list2 is not present in list 1 then only it is pushed into intersection list

push(getIntersections,t1->data,flag);

flag++;

}

t1=t1->next;

}

}

}

int main(){

int num;

printf("Enter the size of the list : \n");

scanf("%d",&num);//enter the size of linked list

array1=malloc(sizeof(struct Node));//allocating memory for linked list 1

insertArray(array1,num);//calling insertarray function to insert list 1 with array1 address

array2=malloc(sizeof(struct Node));//allocating memory for linked list 2

tmp=malloc(sizeof(struct Node));//allocating memory for tempory variable

insertArray(array2,num);//calling insertarray function to insert list 2 with array2 address

display(array1);//displaying the list 1

display(array2);//displaying the list 2

fflush(stdout);

printf("The union of two list is :\n");

getUnion(array1, array2);//calling getunion function

display(getUnions);//displaying unions list

printf("The intersection of two list is :\n");

getIntersection(array1,array2);//calling getintersection function

printf("\n");

display(getIntersections);//displaying intersections list

return 0;

}

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.

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.