Challenge

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

Live Batch(Preeti)

1.

/*
* setsfunctionality.c
*
* Created on: Jan 29, 2023
* Author: Dell
*/

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
//structure is created for the node of linked list
struct Node{
int data;
struct Node* next;//self referential pointer
};
//function declarations
struct Node* unionlist(struct Node* head1,struct Node* head2);
struct Node* intersectlist(struct Node* head1,struct Node* head2);
void insert(struct Node** head_ref,int new_data);
int ispresent(struct Node* head, int data);
void printlist(struct Node* node);
//function for union list
struct Node* unionlist(struct Node* head1,struct Node* head2){ // the function has 2 arguments containing the structure address
struct Node* result=NULL; // structure variables were defined
struct Node* t1=head1;
struct Node* t2=head2;
while(t1!=NULL){ // checking if t1 is not null
insert(&result,t1->data);//inserting the data of 1st linked list into the result list
t1=t1->next;// to traverse through the 1st list
}
while(t2!=NULL){ // checking if t1 is not null
if(!ispresent(result,t2->data)){ // we are using the ispresent function to check if the data in 2nd list is presnt in result list
insert(&result,t2->data);//inserting the data of 2nd linked list into the result list only if the data in 2nd list is not there in the result list which already contains the data of 1st list
}
t2=t2->next;// to traverse through the 2nd list
}
return result;// the unionlist is returned to main function where we call this function.
}
//function for intersection list
struct Node* intersectlist(struct Node* head1,struct Node* head2){// the function has 2 arguments containing the structure address
struct Node* result=NULL;// structure variables were defined
struct Node* t1=head1;
while(t1!=NULL){ // checking if t1 is not null
if(ispresent(head2,t1->data)){ //checking if the data in t1 is present in the 2nd list also
insert(&result,t1->data); // if yes then that data alone is inserted to the result list
}
t1=t1->next;// traversing through the 1st list
}
return result;// the intersection list is returned to main function where we call this function.
}
//function to insert the data into a list
void insert(struct Node** head_ref,int new_data){
struct Node* new_node=(struct Node*)malloc(sizeof(struct Node));//allocating memory for the new node
new_node->data=new_data; // inserting the data into the address of the list
new_node->next=(*head_ref); // pointing to the address ,link the old list off the new node
(*head_ref)=new_node; //move the head to point to the new node
}
//function to check if the data is present in the given list
int ispresent(struct Node* head,int data){
struct Node* t1=head;
while(t1!=NULL){
if(t1->data==data){ // data in the list should match the given data ,then we return 1
return 1;
}
t1=t1->next;
}
return 0;
}
void printlist(struct Node* node){
while(node!=NULL){
printf("%d ",node->data); // printing the data
node=node->next;// traversing through the list
}
}

int main(){
int n,m,temp;
printf("Enter the number of elements in 1st and 2nd list:\n"); // getting the length of the two lists
fflush(stdout);
scanf("%d %d",&n,&m);
struct Node* head1=NULL;//initializing the struct variables with NULL
struct Node* head2=NULL;
struct Node* intersec=NULL;
struct Node* uni=NULL;
for(int i=0;i<n;i++){ // getting the elements of the 1st list
printf("Enter the element no.%d of 1st list:\n",i);
fflush(stdout);
scanf("%d",&temp);
insert(&head1,temp);
}
for(int i=0;i<m;i++){ // getting the elements of the 2nd list
printf("Enter the element no.%d of 2nd list:\n",i);
fflush(stdout);
scanf("%d",&temp);// temp is used to get the elements and store it and then inserting in the list
insert(&head2,temp);
}
intersec=intersectlist(head1,head2);//function call
uni=unionlist(head1,head2);//function call
printf("The 1st list is:\n");
printlist(head1);//function call
printf("\n");
printf("The second list is:\n");
printlist(head2);//function call
printf("\n");
printf("The union list is:\n");
printlist(uni);//function call
printf("\n");
printf("The intersection list is:\n");
printlist(intersec);//function call
}

Output:

Enter the number of elements in 1st and 2nd list:
5 5
Enter the element no.0 of 1st list:
1
Enter the element no.1 of 1st list:
2
Enter the element no.2 of 1st list:
3
Enter the element no.3 of 1st list:
4
Enter the element no.4 of 1st list:
5
Enter the element no.0 of 2nd list:
1
Enter the element no.1 of 2nd list:
2
Enter the element no.2 of 2nd list:
3
Enter the element no.3 of 2nd list:
4
Enter the element no.4 of 2nd list:
6
The 1st list is:
5 4 3 2 1
The second list is:
6 4 3 2 1
The union list is:
6 1 2 3 4 5
The intersection list is:
1 2 3 4

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.