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
Goals
- Creating two linked lists to store the given elements of sets.
- Comparing the given set and creating union and intersection of sets.
Key Highlights of the Project
- Creating the two linked list.
- Inserting the nodes to sort the link list.
- Creating the union and intersection from the linked list.
Expected Outcomes
· Understanding the creating link list, inserting nodes, and displaying the linked list
· Understanding the union and intersection through linked list.
Steps to implement the program
1)Declare 4 link lists for set a, set b, set union, and set intersection;
2)Take the user-defined and sort the data of set A, and Set B and store this data in Link list 1, Link list 2
3)Compare link list 1 and link list 2 and get the union of the set and store it in link list_union.
4) Compare link list 1 and link list 2 and get the intersection of sets of the store it in link list intersection..
5) Display the data of link list _union, link List intersection
*
* creation of user defined data.c
*
* Created on: 28-Dec-2022
* Author: Rakesh
*
*/
//A program to develop set theory using the linked list
// We have to include standard input output library for C language
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
// function to Create Node
struct Node
{
int value;
struct Node *next; // pointer pointing towards next node
};
// initialize the different link list used in the program
struct Node *list1, *list2, *list_union, *list_intersection;
// Function to display the elements of the list
void display_list(struct Node *p)
{
// Checking whether list is empty
if (p == NULL)
{
printf("\nList is empty!!!");
}
// Printing the list
else
{
while(p != NULL)
{
printf("%d ", p->value);
p = p->next;
}
}
}
// Create new node
struct Node *newNode(int value)
{
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = value;
newNode->next = NULL;
return newNode;
}
// insert a node in the sorted linked list.
void insert_node(struct Node **head,struct Node *newNode)
{
// inserting node when List is empty
if(*head == NULL || (*head)->value >= newNode->value)
{
newNode->next = *head;
*head = newNode;
}
// Inserting node in between existing node
else
{
struct Node *current = *head;
while(current->next != NULL && current->next->value < newNode->value)
{
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
// Function to Compare two nodes
int compare(struct Node *list, int data)
{
while(list != NULL)
{
// Comparing values
if(list->value == data)
{
return 1;
}
// If not matched, point to next node address
list = list->next;
}
return 0;
}
struct Node *get_union(struct Node *L1, struct Node *L2)
{
// Initializing union list as NULL
list_union = NULL;
struct Node *temp = L1;
// Copying list 1 data to union list
while(temp != NULL)
{
insert_node(&list_union, newNode(temp->value));
temp = temp->next;
}
// Copying unique data elements of list 2 in union list
while(L2 != NULL)
{
// Comparing list 1 and list 2 data elements
if(!compare(L1, L2->value))
{
// Copying unique elements to union list
insert_node(&list_union, newNode(L2->value));
}
// If matched, point to next node address
L2 = L2->next;
}
// Returning union list
return list_union;
}
struct Node *get_intersection(struct Node *L1, struct Node *L2)
{
// Initializing intersection list as NULL
list_intersection = NULL;
// Comparing list 1 and list 2 data elements for same elements
while(L1 != NULL)
{
// Comparing list 1 and list 2 data elements
if(compare(L2,L1->value))
{
// Copying matched elements to union list
insert_node(&list_intersection,newNode(L1->value));
}
// If not matched, point to next node address
L1 = L1->next;
}
// returning Intersection list
return list_intersection;
}
// Stating of main function
// Logic is written inside this main function
int main()
{
// Initializing list 1 and list 2 as NULL
list1 = NULL;
list2 = NULL;
// Declaring variables
int list1_nodes, list2_nodes;
int i, data;
// Take list 1 nodes input from user
printf("\nEnter number of nodes in list 1: ");
fflush(stdout);
scanf("%d",&list1_nodes);
// Taking list 1 elements from user
printf("\nEnter %d elements of list 1: ",list1_nodes);
for(i = 0; i < list1_nodes; i++)
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list1,newNode(data));
}
// Take list 2 nodes input from user
printf("\nEnter number of nodes in list 2: ");
fflush(stdout);
scanf("%d",&list2_nodes);
// Taking list 2 elements from user
printf("\nEnter %d elements of list 2: ",list2_nodes);
for(i = 0; i < list2_nodes; i++)
{
fflush(stdout);
scanf("%d",&data);
insert_node(&list2,newNode(data));
}
// Display list 1
printf("\nList 1 is ");
display_list(list1);
// Display list 2
printf("\nList 2 is ");
display_list(list2);
// Performing Union of two list
get_union(list1,list2);
// Displaying Union of both list
printf("\nUnion of both list is ");
display_list(list_union);
// Performing Intersection of two list
get_intersection(list1,list2);
// Displaying Intersection of both list
printf("\nIntersection of both list is ");
display_list(list_intersection);
// After executing this main function, this function will return value 0
return 0;
}