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-02#
Project code:
/*
* setmath.c
*
* Created on: Aug 8, 2022
* Author: sunit
*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node//defining the structure
{
int data;//creating the data type of the variable
struct node *next;// creating self referential node
}Node;//changing the structure name as node
static Node *set1,*set2,*set_union,*set_intersection,*newnode,*tail,*res_union,*res_intersection;//declaring the different variable in structure type
/* Declaring diffferent function with the argumrnts*/
Node *insert_node(Node *set_ref,int n);
void display_list(Node *set_ref);
Node *get_union(Node *set_pointer1,Node *set_pointer2);
Node *get_intersection(Node *set_pointer1,Node *set_pointer2);
int compare(Node *set,int data);
void display_list(Node *set_ref)//function used to dispaly the nodes data .
{
if(set_ref == 0)
{
printf(" List is empty.");
}
else
{
tail = set_ref;//we are transfering address if the list to tail
while(tail != 0)
{
printf("%d ", tail->data);// prints the data of current node
tail = tail->next;// advances the position of current node
}
tail = newnode;
}
}
Node *insert_node(Node *set_ref,int n)//function used create the new list
{
printf("\nInserting Elements at the beginning...\n");
newnode =(Node*) malloc(sizeof(Node));//allocating the memory to the nodes
printf("Address of created newnode :%d\n",newnode);
newnode->data = n;//assigning the value to node
printf("Inserted value is :%d\n",newnode->data);
newnode->next = set_ref;//address of the pervious address is transfered to the new node
set_ref = newnode;//transfering the new address to the head ref for linking new node
return set_ref;
}
Node *get_union(Node *set_pointer1,Node *set_pointer2)//function used to union to list
{
while(set_pointer1 != 0)
{
set_union = insert_node(set_union,set_pointer1->data);//here we are inserting the data of list 1 into the union function
set_pointer1 = set_pointer1->next;//we transverse the node using the address in next.
}
while(set_pointer2 != 0)
{
if(!compare(set_union,set_pointer2->data))//Here we are insering the element from list 2 number where not in the in th list1 to the union
set_union = insert_node(set_union,set_pointer2->data);//inserting the element not in the list2
set_pointer2 = set_pointer2->next;//travelling to next node
}
return set_union;
}
int compare(Node *set,int data)
{
while (set != NULL)
{
if (set->data == data)//comparing the data of list 2 with list 1
return 1;
set = set->next;
}
return 0;
}
Node *get_intersection(Node *set_pointer1,Node *set_pointer2)
{
while (set_pointer1 != 0)
{
if (compare(set_pointer2, set_pointer1->data))//we are comparing the both lsit for instersection
set_intersection = insert_node(set_intersection,set_pointer1->data);//transferring the that value to the intersection
set_pointer1 = set_pointer1->next;
}
return set_intersection;
}
int main()
{
int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,
second_list_items = 0;
int f;
printf("Enter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter %d the elements:",first_list_nodes);//getting the number of element in the lists
for( f = 0;f < first_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&first_list_items);//getting the data in run time
set1 = insert_node(set1,first_list_items);//inserting the data in list
}
display_list(set1);//displating the head1 list
printf("\nEnter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter %d the elements:",second_list_nodes);
for(f = 0;f < second_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&second_list_items);//getting the data in run time
set2 = insert_node(set2,second_list_items);
}
display_list(set2);//displating the head1 list
res_union = get_union(set1,set2);//transfering the result of union function to the res union
printf("Here is the result of union: ");
display_list(res_union);//transfring the values as argumnets to display function
res_intersection = get_intersection(set1,set2);//transfering the result of intersection function to the res intersection
printf("Here is the result of intersection: ");//transfering the result of intersection function to the res intersection
display_list(res_intersection);//transfring the values as argumnets to display function
}
