Challenge

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

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

A] Union and intersections of 2 sets using using linked list.

MAIN FILE

/*
* states.c
*
* Created on: 06-Aug-2022
* Author: Vaishnavi
*/


#include <stdio.h> //preprocessor declaration
#include <stdlib.h>
#include <stdbool.h>
#include "states.h" //user defined header file

int main() //main function
{
int list1=0,list2=0,list1_elements=0,list2_elements=0,i;//variable declaration
printf("Enter number of nodes in list1:");//console statement
fflush(stdout);
scanf("%d",&list1);//input statement
printf("Enter nodes in list1 of %d nodes:",list1);//console statement
fflush(stdout);
for(i=0;i<list1;i++)
{
scanf("%d",&list1_elements);//input statement
head1=insert_node(head1,list1_elements);//pushing elements to node
}
print_list(head1);//print values

printf("\nEnter number of nodes in list2:");//console statement
fflush(stdout);
scanf("%d",&list2);//input statement
printf("Enter nodes in list2 of %d nodes:",list2);//console statement
fflush(stdout);
for(i=0;i<list2;i++)
{
scanf("%d",&list2_elements);//input statement
head2=insert_node(head2,list2_elements);//pushing elements to node
}
print_list(head2);//print values

//print values of union and intersection
set_union=get_union(head1,head2);
printf("\nUnion of sets:");
print_list(set_union);

set_intersection=get_intersection(head1,head2);
printf("\nIntersection of sets:");
print_list(set_intersection);

}

HEADER FILE

/*
* states.h
*
* Created on: 06-Aug-2022
* Author: Vaishnavi
*/

#ifndef STATES_H_
#define STATES_H_

//creating node
struct node
{
int data;
struct node *next;
};

//program for printing the result
void print_list(struct node *p)
{
while (p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}

//declaring variables
struct node *head1;
struct node *head2;
struct node *new_node=NULL;
struct node *head_ref=NULL;
struct node *head;
struct node *set_union;
struct node *set_intersection;

//Declaring functions
struct node *get_union(struct node *head1,struct node *head2);
struct node *get_intersection(struct node *head1,struct node *head2);
struct node *insert_node(struct node *head_ref,int n);
bool compare(struct node *head, int data);

//sub function for compare
bool compare(struct node *head, int data)
{
while (head!=NULL)
{
if (head->data==data)
return 1;
head=head->next;
}
return 0;
}

//sub function for creating a node to push value to specific variables
struct node *insert_node(struct node *head_ref,int n)
{
printf("\nInserting elements at the beginning");
new_node=(struct node*)malloc(sizeof(struct node));
printf("\nAddress of created node:%d",new_node);
new_node->data=n;
printf("\nInserted value is:%d\n",new_node->data);
new_node->next=head_ref;
head_ref=new_node;
return head_ref;
}

//sub function for getting union
struct node *get_union(struct node *head1,struct node *head2)
{
struct node *result1 = NULL;
struct node *t1=head1 ,*t2=head2;
while(t1!=NULL)
{
result1=insert_node(result1,t1->data);
t1=t1->next;
}
while(t2!=NULL)
{
if(!compare(result1,t2->data))
result1=insert_node(result1,t2->data);
t2=t2->next;
}
return result1;
}

//sub function for getting intersection
struct node *get_intersection(struct node *head1,struct node *head2)
{
struct node *result2 = NULL;
struct node *t1=head1;
while(t1!=NULL)
{
if(compare(head2,t1->data))
result2=insert_node(result2,t1->data);
t1=t1->next;
}
return result2;
}

#endif /* STATES_H_ */

OUTPUT:

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.