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
#include

typedef struct{ //initialising structure for node
int data;
struct Node *next;
}Node;

//Function declarations
Node *create(Node *head,int num);
void disp(Node *head);
Node *set_union(Node *head1,Node *head2);
int check(Node *newset,int data);
Node *set_intersection(Node *head1,Node *head2);

//main function
int main(){
int m,n,x,y;
Node *head1=NULL;
Node *head2=NULL;
Node *p;
Node *q;

printf("Enter number of nodes for set 1:");
fflush(stdout);
scanf("%d",&m);
for(int i=0;i printf("Enter data for node %d:",i+1);
fflush(stdout);
scanf("%d",&x);
head1=create(head1,x);
}
disp(head1);

printf("\nEnter number of nodes for set 2:");
fflush(stdout);
scanf("%d",&n);
for(int i=0;i printf("Enter data for node %d:",i+1);
fflush(stdout);
scanf("%d",&y);
head2=create(head2,y);
}
disp(head2);

printf("\nUnion of set 1 and set 2:\n");
p=set_union(head1,head2);
disp(p);

printf("\nIntersection of set 1 and set 2:\n");
q=set_intersection(head1,head2);
disp(q);
}

//function to create and add nodes with values
Node *create(Node *head,int num){
Node *temp; //temp used for traversing linked nodes
Node *newnode; //creating separate node
newnode=(Node *)malloc(sizeof(Node));
newnode->data=num;
newnode->next=NULL;

if(head==NULL){
head=newnode;
}
//linking the node to list if list has already been created
else{
temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
return head;
}

//function to print the linked list
void disp(Node *head){
Node *temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}

//function to get union of set 1 and set 2
Node *set_union(Node *head1,Node *head2){
Node *temp=head1;//using temp to not lose data of head1
Node *newset=NULL;
while(temp!=NULL){
if(check(newset,temp->data)){
newset=create(newset,temp->data);}
temp=temp->next;
}

Node *temp1=head2;//using temp1 to not lose data of head2
while(temp1!=NULL){
if(check(newset,temp1->data)){
newset=create(newset,temp1->data);}
temp1=temp1->next;
}
return newset;
}

//function to check whether element is already present in the list
int check(Node *set,int data){
while(set!=0){
if(set->data==data){
return 0;
}
set=set->next;
}
return 1;
}

//function to get intersection of set 1 and set 2
Node *set_intersection(Node *head1,Node *head2){
Node *newset=NULL;
while(head1!=NULL){
if(!check(head2,head1->data)){
newset=create(newset,head1->data);
}
head1=head1->next;
}
return newset;
}

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.