Challenge

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

Offline Batch

#include<stdio.h>
#include<stdlib.h>
//defining the structure
typedef struct
{
int data;
struct node *next;//self referential node
}Node;
//declaring the functions with arguments
Node *insert_node(Node *head_ref, int n);
void display_list(Node *head_ref);
Node *getunion(Node *head_ptr1, Node *head_ptr2);
Node *getintersection(Node *head_ptr1, Node *head_ptr2);
int compare(Node *head, int data);
//declaring the variables in structure
static Node *head, *head1, *head2, *head_union, *head_intersection, *tail, *newnode, *result_union, *result_intersection;
int main()
{
int first_set_nodes=0, first_set_items=0;
int second_set_nodes=0, second_set_items=0;
printf("Enter the number of elements in the 1st set: ");
fflush(stdout);
scanf("%d", &first_set_nodes);
printf("Enter all %d elements: ", first_set_nodes);//getting the elements in the 1st set
for(int i=0; i<first_set_nodes; i++ )
{
fflush(stdout);
scanf("%d",&first_set_items);//getting the data in run time
head1=insert_node(head1,first_set_items);//inserting the data in the 1st set
}
display_list(head1);//displaying the 1st set of elements
printf("\n");
printf("Enter the number of elements in the 2nd set: ");
fflush(stdout);
scanf("%d", &second_set_nodes);
printf("Enter all %d elements: ", second_set_nodes);
for(int i=0; i<second_set_nodes; i++ )
{
fflush(stdout);
scanf("%d",&second_set_items);
head2=insert_node(head2,second_set_items);
}
display_list(head2);//displaying the 2nd set of elements
printf("\n");
result_union=getunion(head1, head2);//calling the function
printf("The union of sets are: ");
display_list(result_union);
printf("\n");
result_intersection=getintersection(head1, head2);//calling the function
printf("The intersection of sets are: ");
display_list(result_intersection);
}
Node *insert_node(Node *head_ref, int n)//function to create a node
{
newnode=(Node*)malloc(sizeof(Node));
newnode->data=n;
newnode->next=head_ref;
head_ref=newnode;
return head_ref;
}
void display_list(Node *head_ref)//function to display the set
{
if(head_ref==0)
{
printf("List is empty!");
}
else
{
tail=head_ref;
while(tail!=0)
{
printf("%d ",tail -> data);//prints the data of current node
tail=tail->next;
}
}
}
Node *getunion(Node *head_ptr1, Node *head_ptr2)//function to get the union of two sets
{
while(head_ptr1!=0)
{
head_union=insert_node(head_union,head_ptr1->data);
head_ptr1=head_ptr1->next;
}
while(head_ptr2!=0)
{
if(!compare(head_union, head_ptr2->data))
head_union=insert_node(head_union,head_ptr2->data);
head_ptr2=head_ptr2->next;
}
return head_union;
}
int compare(Node *head, int data)//function to compare the elements of the two sets
{
while(head!=NULL)
{
if(head->data==data)
return 1;
head=head->next;
}
return 0;
}
Node *getintersection(Node *head_ptr1, Node *head_ptr2)//function to get the intersection of the two sets
{
while(head_ptr1 != 0)
{
if(compare(head_ptr2, head_ptr1->data))
head_intersection=insert_node(head_intersection, head_ptr1->data);
head_ptr1=head_ptr1->next;
}
return head_intersection;
}

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.