Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview -
In this project,
- we created user defined data type
- interfaces for mathematical Set Theory are implemented using C programming language
- we use two Linked List to perform Set operation.
- List data is taken from the user and in the output set operations are performed
- Union and Intersection operation is perform in this program
Goals -
- Implemeting inteface for Set operations using C programming language.
- Creating user defined data type
- Taking user defined input and store in Linked List
- Providing Union and Intersection of two lists as output.
Key Highlights -
- Creating two list of data using Linked Lists.
- Node insertion after sorting the list.
- Performing Union of two lists.
- Getting Intersection of two lists.
Deliverables -
- User defined data type creation for Linked List
- Elements comparison of two lists
- Intersection of data of user defined list
- Union of data of user defined list
Expected Outcome -
- Getting Union and Intersection of data of two lists.
- Comparison of list elements.
- For example,
if List 1 = [1 2 3 4 5] and
List 2 = [4 5 6 7 8], then union and intersection is
union of List 1 and List 2 is [1 2 3 4 5 6 7 8] and
intersection of List 1 and List 2 is [4 5]
Code -
/*
* Project_1.c
*
* Created on: 28-Oct-2022
* Author: Vishal Goyal
*/
// We have to include standard input output library for C language
#include
// for malloc function
#include
// Creating Node
struct Node
{
int value;
struct Node *next; // pointer pointing towards next node
};
// Initializing different list that will be used in this program
struct Node *list1, *list2, *list_union, *list_intersection;
// Function to display 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;
}
}
}
// New Node Creation
struct Node *newNode(int value)
{
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->value = value;
newNode->next = NULL;
return newNode;
}
// Node insertion in 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()
{
/*
* In problem statement it is mentioned that
* we have to create user defined data type to
* implement user interface for working with
* Mathematical Set Theory using Linked List
*/
// Logic Implemented
/*
* Step 1 - Initialize the lists as NULL
* Step 2 - Take user input for number of nodes of both list
* and create new nodes and assign user defined values to them
* Step 3 - Display the lists
* Step 4 - Perform union and display the union list
* Step 5 - Perform intersection and display the list
*/
// 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;
}
Output 1 -

Output 2 -
