Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
Overview of the Project:
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.
Goals:
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key Highlights of the Project:
- Creating two list of data using linked list method.
- Insert the node to 1st and 2nd list accordingly.
- Intersection of data of two lists.
- Getting Union of two list.
Deliverables of this project:
- Intersection of data of two list.
- Union of data of two list.
Program:
//Program that utilizes the linked lists and nodes for giving the union and intersection of two lists
//header files of
//standard boolean library
#include <stdbool.h>
//standard input output library
#include <stdio.h>
//standard C library
#include <stdlib.h>
// Linked list node using a structure
struct Node {
//data as integer
int data;
//creating the node pointer
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 list
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)
{
// allocating the node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// putting in the data
new_node->data = new_data;
// link the old list to the new node
new_node->next = (*head_ref);
// move the head to point 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;
}
// Main program to test above functions
int main()
{
//creating three values n as first list and m as second list
int n,m,temp;
printf("enter the total elements in list1:");
fflush(stdout);
scanf("%d", &n);
printf("enter the total elements in list1:");
fflush(stdout);
scanf("%d", &m);
// Start structure pointer with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersection = NULL;
struct Node* getunion = NULL;
//for loop to link list1
for(int i=0;i<n;i++)
{
printf("Enter the values of list1:");
fflush(stdout);
scanf("%d", &temp);
push(&head1, temp);
}
//for loop to link list2
for(int i=0;i<m;i++)
{
printf("Enter the values of list2:");
fflush(stdout);
scanf("%d", &temp);
push(&head2, temp);
}
//calling the function of intersection
intersection = getIntersection(head1, head2);
//calling the function of union
getunion = getUnion(head1, head2);
printf("\n First list is ");
//function calling with structure pointer as argument
//printing the head1 as it has the list1
printList(head1);
printf("\n Second list is: ");
//printing the head2 as it has list2
printList(head2);
printf("\n Intersection of the two lists is: ");
printList(intersection);
printf("\n Union of the two lists is: ");
printList(getunion);
return 0;
}
Output of the above program

"In the output it seems that the entered numbers are printed in a peculiar order"

Expected Outcomes:
- Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.