Challenge

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

#include <stdio.h>
#include<stdlib.h>
typedef struct ab
{
int a;
struct ab *p;
}b;
int main()
{
int n,m,y,j,c,x=0;
b *set1,*temp;
set1=malloc(sizeof(struct ab));
temp=set1;
printf("enter how many number of elements you want to enter in set1:");
scanf("%d",&n);
printf("start entering the elements in the set:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&(temp->a));
temp->p=malloc(sizeof(struct ab));
temp=temp->p;
}
printf("enter how many number of elements you want to enter in set2:");
scanf("%d",&m);
b *set2;
set2=malloc(sizeof(struct ab));
temp=set2;
printf("star entering the elments int set\n");
for(int i=0;i<m;i++)
{
scanf("%d",&(temp->a));
temp->p=malloc(sizeof(struct ab));
temp=temp->p;
}
temp=set1;
printf("set1:\n");
for(int i=0;i<n;i++)
{
printf("%d ",temp->a);
temp=temp->p;
}
temp=set2;
printf("\nset2:\n");
for(int i=0;i<m;i++)
{
printf("%d ",temp->a);
temp=temp->p;
}
printf("\n");
b *u;
b *i;
b *temp1,*temp2,*temp3,*temp4;
i=malloc(sizeof(struct ab));
temp2=i;
temp=(n>=m)?set1:set2;
temp1=(n<m)?set1:set2;
y=(n>m)?n:m;
temp4=temp;
b *last;
for(int i=0;i<y;i++)
{
last=temp->p;
temp=temp->p;
}
temp=temp4;
for(int i=0;i<(m+n)-y;i++)
{
for(j=0;j<y;j++)
{
c=temp->a;
if(temp1->a==temp->a)
{
temp2->a=temp->a; // Storing common elements in linked list named 'i'
temp2->p=malloc(sizeof(struct ab));
temp2=temp2->p;
x++;
break;
}
temp=temp->p;
}
if(c!=temp1->a)
{
last->a=temp1->a;
last->p=malloc(sizeof(struct ab));
last=last->p;
}
temp1=temp1->p;
temp=temp4;
}
u=malloc(sizeof(struct ab));
temp1=u;
temp=temp4;
c=m+n-(x);
printf("union:\n");
for(int i=0;i<c;i++)
{
printf("%d ",temp->a); // Printing union of set1 and set2
temp=temp->p;
temp1->a=temp->a; // Storing union elements in linked list named 'u'
temp1->p=malloc(sizeof(struct ab));
temp1=temp1->p;
}
printf("\nintersection:\n");
temp=i;
j=0;
while(j<x)
{
printf("%d ",temp->a); // Printing intersection elements
temp=temp->p;
j++;
}
if(j==0)
{
printf("null"); //If there is no common element then it prints null
}
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.