Challenge

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

#include<stdio.h> //preprocessor for i/o header file
#include<stdlib.h>//preprocessor with standard library for malloc()

struct node //struct defined
{
int data;
struct node *next;
} *head1, *head2, *head_union, *head_intersection; //structure members and variables

void init() //called function
{
head1=head2=NULL; //declaring head1 & head2 as NULL
}
void insert_node(struct node **head, int num) //called function for adding lists
{
struct node *newnode=(struct node*)malloc(sizeof(struct node)); //dynamic memory allocation
newnode->data=num; //pointing to the data using pointer
newnode->next=*head; //pointing next address to *head pointer
*head=newnode; //handling head to new_node
}
int search(struct node *ptr, int num) //called function for searching element
{
while(ptr!=NULL) //iterating till ptr not equal to NULL
{
if(ptr->data==num){ //checking if ptr->data consists of num
return 1;
} //if yes returning yes
ptr=ptr->next; //pointing ptr to next for another iteration
}
return 0; //returning zero for return type 'int'
}
struct node *find_union(struct node *head1, struct node *head2) //called function for finding union
{
head_union=NULL; //head_union as NULL
struct node *temp=head1; //storing head1 in temp pointer
while(temp!=NULL) //iterating till temp not equal to NULL
{
insert_node(&head_union, temp->data); //inserting temp data
temp=temp->next; //pointing temp to next for another iteration
}
while(head2!=NULL) //iterating till head2 not equal to NULL
{
if(!search(head1, head2->data)){ //searching head1 pointer and head2 pointer data
insert_node(&head_union, head2->data); //if above condition true inserting head_union and head2 data
}
head2=head2->next; //pointing head2 to next for another iteration
}
return head_union; //returning head_union for fucntion return type struct node 'find_union'
}
struct node *find_intersection(struct node *head1, struct node *head2) //called function for finding intersection
{
head_intersection=NULL; //making head_intersection as NULL
while(head1!=NULL) //iterating till head2 not equal to NULL
{
if(search(head2, head1->data)){ //if searching goes true for head2 pointer and data in head2 pointer
insert_node(&head_intersection, head1->data); //if above condition satisfies inserting head_intersection and head1 data
}
head1=head1->next;//pointing head1 to next for another iteration
}
return head_intersection;//returning head_intersection for fucntion return type struct node 'find_intersection'
}
void print_list(struct node *ptr) //called function for printing lists
{
while(ptr!=NULL) //iterating till head2 not equal to NULL
{
printf("%d ", ptr->data); //o/p statement for printing data in ptr pointer
ptr=ptr->next; //pointing ptr to next for another iteration
}
printf("\n"); //new line
}

int main() { //main function
int s1, s2; //variables for two list sizes
int item1, item2; //variables for elements in the lists
printf("Enter the size of List 1: "); //o/p statement
scanf("%d", &s1); //taking input for list 1 size
for(int i=0;i<s1;i++) //iterating the loop till i becomes less than s1 i.e size of list one
{
scanf("%d", &item1); //taking elements as input
insert_node(&head1, item1); //inserting values by calling function 'insert_node'
}
printf("Enter the size of List 2: "); //o/p statementt
scanf("%d", &s2);//taking size_2 for list 2
for(int i=0;i<s2;i++)//iterating the loop till i becomes less than s2 i.e size of list two
{
scanf("%d", &item2); //taking elements as input
insert_node(&head2, item2);//inserting values by calling function 'insert_node'
}
printf("--------------------------------------------\n");//dividing lines output
printf("The Values of First Linked List is "); //o/p statement
print_list(head1); //calling function print_list for printing outputs
printf("The Values of Second Linked List is ");
print_list(head2);//calling function print_list for printing outputs
find_union(head1, head2); //calling function for getting union
printf("--------------------------------------------\n");
printf("Union of Linked List is \n");
print_list(head_union);//calling function print_list for printing outputs
find_intersection(head1, head2);//calling function for getting intersection
printf("Intersection of Linked List is \n");
print_list(head_intersection);//calling function print_list for printing outputs
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.