Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*
* Project-1.c
*
* Created on: 03-Aug-2023
* Author: RAJASEKAR.R
* Description: User defined data type to implement the mathematical set theory using linkedlist.
*/
// preprocessor declaration
#include
#include
#include
// Initializes structure data type
typedef struct Node{
// Declared the integer data type
int value;
// Self-referential structure its points the address of the nsxt node data
struct Node* next;
}node; //Alias name of the structure
// Initialize the structure pointer as null and declare as global variable
node *list1, *list2 = NULL;
node *head_un, *head_is = NULL;
node *res_un, *res_is = NULL;
//Function for creating a linkedlist
node *insert_list(node *head_list, int data)// The function parameter as pointer variable and integer variable
{
// Creating a newnode and allocate memory using runtime type memory allocation
node*new_node = (node *)malloc(sizeof(node));
new_node->value = data; //The data store in the value of the structure.
new_node->next = head_list; // The pointer points the address
head_list = new_node;
return head_list;
}
// Function for displaying the list.
void printlinkedlist(node *head_list)// Function parameter as pointer
// function body
{
// Create a new pointer and assing value as head of the list.
node*first=head_list;
if(head_list==NULL) // Condition check if the head equal to null the list is empty.
{
printf("List is empty");
}
while(first!=NULL) // Condition check
{
// It display the address of the node and the value stored in the address
printf("\nAddress=%u\tdata=%d",&first->value,first->value);
first = first->next;// The head points the next address.
}
printf("\n");
}
// Function for compare if the data is present in the list it returns true or else return false.
int compare(node *ptr, int data) // Function parameters are pointer and integer data
// Function body
{
while(ptr != NULL) // Function checks using while loop traverse list not equal to null.
{
if(ptr->value == data)
{
return 1; // Function successfully executed and returning 1
}
ptr = ptr->next; // The pointer points the next address of the node.
}
return 0;
}
// Function for to get union of the list1 and list2 get by the user.
node *value_union(node *ptr1, node *ptr2) //Function parameter as to get two pointers for list1 and list2.
//Function body
{
while(ptr1 !=0)// Function checks using while loop list1 not equal to null.
{
head_un = insert_list(head_un,ptr1->value);// List1 data stores in the result list
ptr1 = ptr1->next; //It points the next node address
}
// insert those element in the list2 and which are not present in the result list
while(ptr2 !=0) // while loop list2 is not equal to null.
{
// condition check list2 is not present in the result list.
if(!compare(head_un, ptr2->value))
{
head_un = insert_list(head_un, ptr2->value); // list2 Data stores in the result list.
}
ptr2 = ptr2->next;// It points the next node address.
}
return head_un; // Function execute successfully it returns the result list.
}
// Function for to get intersection of the list1 and list2 get by the user.
//If the same element present in the list1 and list2 then insert in the result list.
node *value_intersection(node *ptr1, node *ptr2)
// Function body
{
while(ptr1 != 0)// while loop list1 is not equal to null.
{
if(compare(ptr2,ptr1->value)) // The data transverse to list1 into head of the list2.
{
head_is = insert_list(head_is, ptr1->value);// list1 Data stores in the result list.
}
ptr1 = ptr1->next; // It points the next data.
}
return head_is;// Function execute successfully it returns the result list.
}
int main()//Above functions are test in the main function
{
int lt1,lt2; // Local variable declare for to get the number of the nodes in the list.
int i, data1=0, data2 =0 ; // Local variable for to get the data for the list.
setbuf(stdout,NULL); // For clearing the console buffer
//Number of nodes in the list1 get by user
printf("\n enter the no.of.nodes for list1:");
scanf("%d",<1>
//For list1 data are get by user using for loop.
for(i=0;i
{
printf("Enter the list_1 data_%d_:",i+1);
scanf("%d",&data1);
list1 = insert_list(list1,data1);// Function call and it stores in the list1
}
// Displaying the list1 data where get by user.
printf("\nThe List1 nodes address and data are:");
printlinkedlist(list1);//Function call to display the list2 data in the list.
//Number of nodes in the list2 get by user
printf("\n enter the no.of.nodes for list2:");
scanf("%d",<2>
//For list2 data are get by user using for loop.
for(i=0;i
{
printf("Enter the list_2 data_%d_:",i+1);
scanf("%d",&data2); // The data stores in the data2.
list2 = insert_list(list2,data2);// Function call and it stores in the list2
}
// Displaying the list2 data where get by user.
printf("\nThe List2 nodes address and data are:");
printlinkedlist(list2);//Function call to display the list2 data in the list
// Displaying the union result between list1 and list2.
printf("\n...The Result of union between list1 and list2 are...\n");
res_un = value_union(list1,list2);// Function call to get the union between list1 and list2 get by the user and it stores in the result..
printlinkedlist(res_un);//Function call to display the union result
// Displaying the intersection result between list1 and list2.
printf("\n...The Result of intersection between list1 and list2 are...\n");
res_is = value_intersection(list1,list2);// Function call to get the intersction between list1 and list2 get by the user and it stores in the result..
printlinkedlist(res_is);// Function call to display the intersection result.
}
OUTPUT:

