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 section
#include<stdlib.h>
#include<stdbool.h>

struct Node{ // creating the Linked list node
int Value;

struct Node*next;

};
void push(struct Node**head_ref,int new_Value);// calling functions
int elementispresent(struct Node*head,int Value);//calling functions
void push(struct Node**head_ref,int new_Value){ // called function of push
struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));
new_node->Value=new_Value;
new_node->next=(*head_ref);
(*head_ref)=new_node;
}
int elementispresent(struct Node*head,int Value){// elementispresent function
struct Node* P=head;
while(P!=NULL){
if(P->Value==Value){
return 1;
}
P=P->next;
}
return 0;
}
struct Node* getIntersection(struct Node*head1,struct Node*head2 ){
struct Node*result=NULL; // Intersection function ,comparing the linked list functions
struct Node*P1=head1;
while(P1!=NULL){
if( elementispresent(head2,P1->Value)){
push(&result,P1->Value);
}
P1=P1->next;
}return result;
}
struct Node* getUnion(struct Node*head1,struct Node*head2 ){// Union function creating for the union of the linked lists
struct Node*result=NULL;
struct Node*P1=head1;
struct Node*P2=head2;
while(P1!=NULL){
push(&result,P1->Value);
P1=P1->next;
}
while(P2!=NULL){
if(!elementispresent(result,P2->Value)){
push(&result,P2->Value);
}
P2=P2->next;
}return result;
}
void printlist(struct Node*Node){ // printing the results, creating the printlist function
while(Node!=NULL){
printf("\n %d",Node->Value);
Node=Node->next;
}
}
int main(){// the main function starts
int n,m; // initializing the variables
int temp;
printf("\n ***Linked lists*** : ");
printf("\n ***** \n");
printf("\n The number of elements present in the linked lists: ");
fflush(stdout);
scanf("%d %d",&n,&m);
struct Node*head1=NULL;
struct Node*head2=NULL;
struct Node*unin=NULL;
struct Node*intersecn=NULL;
for(int i=0;i<n;i++) // crating the first linked list1
{
printf(" Enter the elements in the linkedlist1 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head1,temp);}
for(int i=0;i<m;i++) // creating the second linked list
{
printf(" Enter the elements in the linkedlist2 %d:-",i);
fflush(stdout);
scanf("%d",&temp);
push(&head2,temp);
}unin=getUnion(head1,head2);// calling the union function
intersecn=getIntersection(head1,head2);// calling the intersection function
printf("\n The elements of the linkedlist1 are ");
printlist(head1); // printing the elements in the linked list
printf("\n The elements of the linkedlist2 are ");
printlist(head2);// printing the elements in the linked list
printf(" \n The Union elements ");
printlist(unin);
printf(" \n The Intersection elements ");
printlist(intersecn);
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.