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>//preprocessor directives
#include<stdlib.h>
typedef struct node{ //Defining a structure node
int data; //integer variable for storing the data
struct node* link; //pointer variable for storing address
}Node;//alias name
//declaring 10 varibales of type Node
static Node *h1,*h2,*head,*tail,*union1,*intersection,*result_union,*result_intersection, *headnode,*newnode;
//Function prototypes
Node *insert(Node* a,int n);
void display(Node *b);
Node *union2(Node *h,Node *c);
int compares(Node *base,int f);
Node *intersection1(Node *g,Node *j); //
int main(){
int n,m,fl,sl;//declaring 4 varibales
printf("Enter number of nodes for !st list\n");
scanf("%d",&n);//read inputs from user
printf("Enter elements to be inserted\n");
for(int i=0;i<n;i++){ //loop for inserting elements into first list
scanf("%d",&fl);
h1=insert(h1,fl);//Functioncall

}
display(h1); //function call
printf("Enter number of nodes for 2nd list\n");
scanf("%d",&m);
printf("Enter elements to be inserted\n");
for(int i=0;i<m;i++){//loop for inseting elements into second list
scanf("%d",&sl);
h2=insert(h2,sl);//functiion call

}
display(h2);//function call
printf("union ");
result_union=union2(h1,h2);//function call
display(result_union);//function call
printf("intersection " );
result_intersection=intersection1(h1,h2);//function call
display(result_intersection);//function call
}
//function defintion for insert
Node *insert(Node *headnode,int db){
newnode=(Node*) malloc(sizeof(Node));
newnode->data =db;
newnode->link=headnode;
headnode=newnode;
return headnode;
}
//function defintion for insert
void display(Node *head){
//struct node* temp; //creating a temporary node
tail=head; //copying root value to the temp
//checking for if list is empty
if(tail == NULL){
printf("list is empty");
}else{ //printing elements in list
printf("elements are\n");
//loop runs till the link address is NULL
while(tail!=NULL){
printf("%d ",tail->data); //printing data to console
tail=tail->link; //incrementing the link position to point to next node
}
printf("\n");
}
}
//function defintion for insert
Node *union2(Node *head1,Node *head2){
while(head1!=NULL){
union1= insert(union1,head1->data);//function call and storing result in union1 varibale
head1=head1->link;//incrementing to next location
}
while(head2!=NULL){
if(!compares(union1,head2->data)) //function call in if loop
union1=insert(union1,head2->data); ////function call and storing result in union1 varibale
head2= head2->link;//incrementing to next location
}
return union1;
}
//function defintion for compares
int compares(Node *d,int e){
while(d!=NULL){
if(d->data == e)
return 1;
d=d->link;

}
return 0;
}
//function defintion for intersection1 and retun intersection
Node *intersection1(Node* head1,Node* head2){
while(head1!=NULL){
if(compares(head2,head1->data))
intersection=insert(intersection,head1->data);
head1=head1->link;
}
return intersection;


}

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.