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 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked
Overview of the project:
Here the main objective of the project is to do the mathematical set operation. The set operation has many operations here they are told to do the union and intersection of the list crerated as a linked list. so here we are creating a structure it has a collecion different data. Here is the process to first creare two list using the reference address. The main concept of linkedlist is self referential structure.
Implementation Of C interface for mathematical set theory using C progamming:
we are creating two datatypes int for storing the integer value, and the pointer variable is used it storing the address of the same structure itself.
And after we are creating the main function we get the values of list 1 anf list 2 using the for loop and using the many function call in that.
we are creating diff function.
Creating two lists of data using the linked list method:
First, we are getting the size of the list. Using the for loop we are inserting the data inside the for loop, we use the insert node function.
In these functions, we are using arguments are head address and value from the user.
Inside the inserted node, we are creating the size of the list using the malloc funciton.
Entering the data in the data area, and storing the address of the head to the next pointer and we are storing the new node address in the head for the next update.
Likewise, we are creating the two lists.
Getting Union of two lists:
Here, we get a function call of getting union with the argument of head1 and head 2
In the union function , we are using the head unit as a list by inserting the value of the first list in the union and jumping to the next area by addressing stores in the next.
Insert node function as same as the function which is used for the creation of the inked list
Getting Intersection of two lists.
In the intersection, we are easy to get an intersection because just we compare to list and return the value which is placed in both lists commonly.
Using the if command comparing the data of lists 1 and 2
If the condition is true it passes the next step which has an intersection list and using the insert function we are inserting the data into that list.
C-PROGRAM
//project 1-Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Link list node
struct Node {
int data;
struct Node* next;
};
//function declaration
void push(struct Node** head_ref, int new_data);
int isPresent(struct Node* head, int data);
// Function to get union of two linked lists head1 and head2
struct Node* getUnion(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
// Insert all elements of list1 to the result list
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
{
push(&result, t2->data);
}
t2 = t2->next;
}
return result;
}
// Function to get intersection of two linked lists head1 and head2
struct Node* getIntersection(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
// Traverse list1 and search each element of it in list2. If the element is present in list 2, then insert the element to result
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
{
push(&result, t1->data);
}
t1 = t1->next;
}
return result;
}
// A utility function to insert a node at the beginning of a linked list
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// put in the data
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*head_ref);
// move the head to point to the new node
(*head_ref) = new_node;
}
// function to print a linked list
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
// function that returns true if data is present in linked list else return false
int isPresent(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)
{
return 1;
}
t = t->next;
}
return 0;
}
// Driver program to test above function
int main()
{
int n,m,temp;
printf("enter the value of number of value in link list(both):-");
fflush(stdout);
scanf("%d %d", &n, &m);
// Starth structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
//for loop for link list1
for(int i=0;i<n;i++)
{
printf("Enter the value in link list1 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp);
}
//for loop for link list2
for(int i=0;i<m;i++)
{
printf("Enter the value in link list2 %d:-",i);
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp);
}
//call the function of intersection
intersecn = getIntersection(head1, head2);
//call the function of union
unin = getUnion(head1, head2);
printf("\n First list is ");
//function calling with structure pointer as argument
printList(head1);
printf("\n Second list is ");
printList(head2);
printf("\n Intersection list is ");
printList(intersecn);
printf("\n Union list is ");
printList(unin);
return 0;
}
Output of program:
