Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*
* project_one.c
*
* Created on: 03-Sep-2022
* Author: Rishabh Dev Shukla
*/
// C program to find union and intersection of two linked lists
#include //Preprocessor section. Standard i/o header file includes predefined functions and library like printf.
#include // Preprocessor section. Includes predefined functions and library for boolean operation of boolean datatype
#include //Preprocessor section. defines several macros,and various functions for performing general functions like memory allocation.
typedef struct node{ //defining the structure// creating a node in linked list using struct
int num; // int type Data part of linked list
struct node *ptr1; // Pointer ptr1 pointing or holding the next node address and creating self referential type
}nodee; //creating alias (structure) name as nodee
static nodee *hd_one,*hd_two,*hd,*hd_uon,*hd_intsect,*add_node,*link,*result_uon,*result_intsect;//declaring the different variable in structure type
nodee *add_nodee(nodee *hd_ref,int n); // Declaration of the Add node function with the arguments
void display_nodee(nodee *hd_ref); // Declaration of the display function with the arguments
nodee *uon_nodee(nodee *hd_ptr1,nodee *hd_ptr2); // Declaration of the union function with the arguments
nodee *intsect_nodee(nodee *hd_ptr1,nodee *hd_ptr2);
int compare(nodee *hd,int num);
void display_nodee(nodee *hd_ref) //definition of function used to display the linked list nodes data .
{
if(hd_ref == 0) //Comparing pointer hd_ref with zero
{
printf(" List is empty.");
}
else
{
link = hd_ref;//we are linking address
while(link != 0)
{
printf("%d ", link->num);// prints the data of present node
link = link->ptr1;// advances the position of present node
}
link = add_node;
}
}
nodee *add_nodee(nodee *hd_ref,int n)//declaration and definition of function used create the new list
{
printf("n Add data at the beginning->n");
add_node =(nodee*) malloc(sizeof(nodee));//Dynamic memory allocation of the nodes
printf("Address of new added node :%dn",add_node);
add_node->num = n;//passing the value n to the node
printf("Added value of the data is :%dn",add_node->num);
add_node->ptr1 = hd_ref;//address is transfered to the new added node
hd_ref = add_node;//transfering the new address to the hd_ref for linking new added node
return hd_ref;
}
nodee *uon_nodee(nodee *hd_ptr1,nodee *hd_ptr2)//declaration and definition of the function used to do union
{
while(hd_ptr1 != 0)
{
hd_uon = add_nodee(hd_uon,hd_ptr1->num);//adding the data of list 1 into the union function
hd_ptr1 = hd_ptr1->ptr1;//traverse the node using the address in next.
}
while(hd_ptr2 != 0)
{
if(!compare(hd_uon,hd_ptr2->num))//adding the element from list 2 number those not in the in the list1 to the union
hd_uon = add_nodee(hd_uon,hd_ptr2->num);//adding the element not in the list2
hd_ptr2 = hd_ptr2->ptr1;//linking to the next node
}
return hd_uon;
}
int compare(nodee *hd,int num)
{
while (hd != NULL)
{
if (hd->num == num) //comparing the data of list 2 with list 1
return 1;
hd = hd->ptr1;
}
return 0;
}
nodee *do_intsect(nodee *hd_ptr1,nodee *hd_ptr2) //declaration and definition of the function used to do intersection
{
while (hd_ptr1 != 0)
{
if (compare(hd_ptr2, hd_ptr1->num))//we are comparing the both lsit for instersection
hd_intsect = add_nodee(hd_intsect,hd_ptr1->num);//transferring the that value to the intersection
hd_ptr1 = hd_ptr1->ptr1;
}
return hd_intsect;
}
int main()
{
int list1_nodes = 0,list2_nodes = 0,list1_elems = 0,
list2_elems = 0;
int i;
printf("Enter the number of Nodes in the List1:");
fflush(stdout);
scanf("%d",&list1_nodes);
printf("Enter number of the elements for List1: %d",list1_nodes);//getting the number of element in the lists
for( i = 0;i< list1_nodes;i++)
{
fflush(stdout);
scanf("%d",&list1_elems);//getting the data in run time
hd_one= add_nodee(hd_one,list1_elems);//adding the data in list1
}
display_nodee(hd_one);//Displaying the hd_one list
printf("nEnter the number of Nodes in the List2:");
fflush(stdout);
scanf("%d",&list2_nodes);
printf("Enter number of the elements for List2: %d",list2_nodes);
for(i= 0; i<list2_nodes;i++)
{
fflush(stdout);
scanf("%d",&list2_elems);//getting the data from console in run time for list2
hd_two = add_nodee(hd_two,list2_elems);
}
display_nodee(hd_two);//Displaying the hd_one list
result_uon = uon_nodee(hd_one,hd_two);//Transferring the result of union function to the res union
printf("The result of union is: ");
display_nodee(result_uon);//Transferring the values as arguments to display function
result_intsect = do_intsect(hd_one,hd_two);//transfering the result of intersection function to the res intersection
printf("Here is the result of intersection: ");//Transferring the result of intersection function to the res intersection
display_nodee(result_intsect);//Transferring the values as arguments to display function
return 0;
}