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>//header file for input output function utility
#include<stdlib.h>//header file for general purpose utility for example memory allocation
#include<stdbool.h>//header file for boolean use

//creating a linked node
struct node{
int data;
struct node* next;
};
//all prototype function declaration
//creating a function for new node insertion
void push(struct node**head_ref,int new_data);
//creating function which is return union in linked list form
struct node* get_union(struct node*head1, struct node*head2);
//creating function which is return intersection in linked list form
struct node* get_intersection(struct node*head1, struct node*head2);

void display(struct node*head);//display function
//this function is use for return true false using bool
bool ispresent(struct node*node,int n);


void push(struct node**head_ref,int new_data){
//struct node size memory allocation by using malloc function
//dynamically memory allocation
struct node*new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=new_data;
new_node->next=(*head_ref);//insert a newnode in begining of list
(*head_ref)=new_node;
}

struct node* get_union(struct node*head1, struct node*head2){
//t1 and t2 is use for list traversing
struct node*t1=head1;
struct node*t2=head2;
struct node*result=NULL;

while(t1!=NULL){
//all list one data store in result list
push(&result,t1->data);
t1=t1->next;
}
while(t2!=NULL){
//function is present is use to check whether data is already present in list or not
if(!ispresent(result,t2->data))
//data is not present in list this function will be insert those numbers into the result list
push(&result,t2->data);
t2=t2->next;
}
return result;
}
//function is use to get common data(intersection) in both list
struct node* get_intersection(struct node*head1, struct node*head2){
struct node*t1=head1;

struct node*result=NULL;
while(t1!=NULL){
//bool function is use to check whether data is present in both list or not
//pass list2 head as argument and check list 1 values using data traversing node t1
if(ispresent(head2,t1->data))
//if data is present in both list insert the same data in result
push(&result,t1->data);
t1=t1->next;
}
return result;//function return type is struct node so return struct node type result
}
//this function is use for display the list
void display(struct node*node){
if(node==NULL)
//if memory is not allocated this function will be shown null
printf("memory empty\n");
else{
//otherwise print the list array
while(node!=NULL){
printf("%d\n",node->data);
node=node->next;
}
}
}
//this function is use to check data present or not
bool ispresent(struct node*ptr,int n){
struct node*t=ptr;
while(t!=NULL){
if(t->data==n){
//if data is present return 1 true otherwise it will be return 0 false
return 1;}
t=t->next;
}
return 0;
}
int main()
{
int a,b,c,d,w,x,y,z; //integer variable store in link list
//all list is null initially
struct node* head1=NULL;
struct node* head2=NULL;
struct node* unin=NULL;
struct node* intrscn=NULL;
//asking for input elements LL1
printf("\n Enter the 4 element LL1\n");
fflush(stdout);
scanf("\n%d",&a);
scanf("\n%d",&b);
scanf("\n%d",&c);
scanf("\n%d",&d);
//asking for input elements LL2
printf("\n Enter the 4 element LL2\n");
fflush(stdout);
scanf("\n%d",&w);
scanf("\n%d",&x);
scanf("\n%d",&y);
scanf("\n%d",&z);
//create LL1
push(&head1,a);
push(&head1,b);
push(&head1,c);
push(&head1,d);
//create LL2
push(&head2,w);
push(&head2,x);
push(&head2,y);
push(&head2,z);
//using function get union we will get union of the lists
unin=get_union(head1,head2); //pass as arguments both list head
//using function get intersection we will get common value of the lists
intrscn=get_intersection(head1,head2); //pass as arguments both list head
printf("\n LL1\n");
//display first list using function display
display(head1);
printf("\n LL2\n");
//display second list using function display
display(head2);
printf("\n union is:\n");
//display union using function display
display(unin);
printf("\n intersection is:\n");
//display intersection using function display
display(intrscn);
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.