Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists
AIM:
To write a C program to implement the interfaces for mathematical Set Theory in which user need to give the inputs like two list of data and the program will give the output as the union and intersection of these two lists.
ALGORITHM:
Step 1: We need to create a project and source file in the STM cube IDE.
Step 2: We need to declare the required header files and 5 function prototype is also declared named as "insert", "Display_Node", "union_node", "intersection_node" and "compare".
Step 3: We need to create a structure named as "Node" with an user-defined datatype "typedef" in order give a alias name to the structure.
Step 4: We need to declare the pointer variable to the datatype structure named as "Node" to perform union and intersection of two list. so the below variables are declared namely "h1", "h2", "head", "tail", "union_list", "intersection", "result_union", "result_intersection", "headnode" and "newnode".
Step 5: In main function we are declaring 5 interger variables named as "total_first", "total_second", "first_list_value", "second_list_value" and "i".
Step 6: The variables "total_first", "total_second" is used to get the number of nodes in the 1st list and 2nd list from the user using scanf() function.
Step 7: The variables "first_list_value", "second_list_value" is used to get the elements of node 1 and node 2 from the user using scanf() function. The variable "i" is used to get the each elements of node using For loop.
Step 8: In For loop we are calling a function called "insert" with arguments like address of node 1(h1) and 1st element value(first_list_value) and the elements of 1st node is displayed using function named as "Display_Node()" with the argument of h1 address.
Step 9: The Step 8 is followed for inserting the elements in the node 2 and displaying the elements of node 2.
Step 10: We are assigning the function calls like "union_node()" and "intersection_node()" in the variable "result_union" and "result_intersection" respectively. The base address of node 1 (h1) and node 2(h2) is passed as arguments.
Step 11: Using the function call named as "Display_Node()" we are printing the union and intersection of two list. Here the arguments like "result_union" and "result_intersection" is used.
Step 12: In the function definition of insert() function we are providing the return type as "Node" and arguments as "Node *headnode", "int n".
Step 13: Using malloc we are dynamically creating a memory space for the variable "newnode" and storing the value and address in the new node variable. Also we are storing the address of the new node variable to headnode variable and returing the headnode value.
Step 14: In the function definition of Display_Node() function we are providing the return type as "void" and arguments as "head".
Step 15: Using If condition we are checking whether the head is NULL and if it is NULL we will get the output as "List is empty" or else we are storing the "head" value to the variable "tail".
Step 16: Using while loop we are checking whether the tail value is not equal to NULL, when this condition satisfies we are getting the linked list values.
Step 17: In the function definition of union_node() function we are providing the return type as "Node" and arguments as "Node *h1", "Node *h2".
Step 18: Using While loop we are inserting the list 1 values to the union_list and also using another While loop we are comparing the union_list values and list 2 values.
Step 19: Here we are also returing the union_list linked list to main function which is get stored in the variable "result_union".The comparison is done using another function called "compare".
Step 20: In the function definition of intersection_node() function we are providing the return type as "Node" and arguments as "Node *h1", "Node *h2".
Step 21: Using While loop we are comparing the list 2 values and list 1 values and returing the intersection linked list to main function which is get stored in the variable "result_intersection". The comparison is done using another function called "compare".
Step 22: In the function definition of compare() function we are providing the return type as "int" and arguments as "Node *headnode", "int n".
Step 23: Using While loop we are checking headnode is not equal to NULL and if the condition satisfies then we are comparing the 2nd linked list value with 1st linked list value.
Step 24: When the above If loop is true it will return the value as "1" which in turn gives the intersection of two linked list and if both the linked list values are not equal it will return the value as "0" which in turn gives the union of two linked list.
CODE:
/*
* link.c
*
* Created on: 10-Sep-2022
* Author: Jagadesh R
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int value;
struct node *next;
}Node;
static Node *h1;
static Node *h2;
static Node *head;
static Node *tail;
static Node *union_list;
static Node *intersection;
static Node *result_union;
static Node *result_intersection;
static Node *headnode;
static Node *newnode;
Node *insert(Node *headnode, int n);
void Display_Node(head);
Node *union_node(Node *h1, Node *h2);
Node *intersection_node(Node *h1, Node *h2);
int compare(Node *headnode, int n);
int main()
{
int total_first;
int total_second;
int first_list_value;
int second_list_value;
int i;
printf("\nEnter the nodes to be created in 1st node:");
fflush(stdout);
scanf("%d",&total_first);
printf("\nEnter the nodes to be created in 2nd node:");
fflush(stdout);
scanf("%d",&total_second);
fflush(stdout);
for(i=0;i<total_first;++i)
{
printf("Enter the elements of the 1st node:");
fflush(stdout);
scanf("%d",&first_list_value);
h1 = insert(h1,first_list_value);
}
Display_Node(h1);
for(i=0;i<total_second;++i)
{
printf("\nEnter the elements of the 2nd node:");
fflush(stdout);
scanf("%d",&second_list_value);
h2 = insert(h2,second_list_value);
}
Display_Node(h2);
result_union = union_node(h1,h2);
printf("\nUnion of linked list:");
Display_Node(result_union);
result_intersection = intersection_node(h1,h2);
printf("\nIntersection of linked list:");
Display_Node(result_intersection);
}
Node *insert(Node *headnode,int n)
{
newnode = (Node *) malloc (sizeof(Node));
newnode->value = n;
newnode->next = headnode;
headnode = newnode;
return headnode;
}
void Display_Node(head)
{
if(head == NULL)
{
printf("\n List is empty");
}
else
{
tail = head;
while(tail != NULL)
{
printf("\nLinked list values are:%d",tail->value);
tail = tail->next;
}
}
}
Node *union_node(Node *h1, Node *h2)
{
while(h1 != NULL)
{
union_list = insert(union_list,h1->value);
h1 = h1->next;
}
while(h2 != NULL)
{
if(!(compare(union_list,h2->value)))
{
union_list = insert(union_list,h2->value);
}
h2 = h2->next;
}
return union_list;
}
Node *intersection_node(Node *h1, Node *h2)
{
while(h1 != NULL)
{
if(compare(h2,h1->value))
{
intersection = insert(intersection,h1->value);
}
h1 = h1->next;
}
return intersection;
}
int compare(Node *headnode, int n)
{
while(headnode != NULL)
{
if((headnode->value) == n)
{
return 1;
}
headnode = headnode->next;
}
return 0;
}
RESULT:
Thus the C program is written to implement the interfaces for mathematical Set Theory in which user need to give the inputs like two list of data and the program will give the output as the union and intersection of these two lists.
