Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview of the Project: (5 to 6 lines)
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:
Goals (2 to 3 points)
- 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: (4 to 5 point)
- 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: (4 to 5 points)
- Intersection of data of two list.
- Union of data of two list
Expected Outcomes: (4 to 5 points)
- Student will get the Knowledge of creating the nodes using Linked List.
- Getting the intersection and Union of data of two list.
- Comparing the elements of two list.
Solution:
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 lisr. so here we are creating a structure it has a collecion different dara. Here is the process to first creare two lisrs using the reference address. The main concept of linkedlist is self referential structure.
Implementation Of C interface for mathematical set theory using C progamming:
First, we are creating the structure as a tyoedef model to rename the user defined name.
In that, 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.
And then we are creating the different functions wha are mentioned in the main program.
Creating two lists of data using the linked list method:
Firsr, we are getting the size of the list. Using the for loop we are inserting the data inside the foe loop, we use the insert node function.
In these functions, we are using arguments are heas address and value from the users
#include <stdio.h>
#include <stdlib.h>
// A structure of linked list node
struct node
{
int data;
struct node *next;
} *LLOne, *LLTwo, *unionLL, *intersectionLL;
void initialize()
{
LLOne = LLTwo = NULL;
}
//Given a Inserts a node in front of a singly linked list.
void insert(struct node **head, int num)
{
// Create a new Linked List node
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = num;
// Next pointer of new node will point to head node of linked list
newNode->next = *head;
//make new node as new head of linked list
*head = newNode;
}
// Searches an element in Linked List by linearly traversing from head to tail
int search(struct node *head, int num)
{
while (head != NULL)
{
if (head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
//Returns the union of two given linked list
struct node* findunion(struct node *LLOne, struct node *LLTwo)
{
unionLL = NULL;
// Add all nodes of first Linked List to unionLL
struct node *temp = LLOne;
while(temp != NULL)
{
insert(&unionLL, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(LLTwo != NULL)
{
if(!search(LLOne, LLTwo->data))
{
insert(&unionLL, LLTwo->data);
}
LLTwo = LLTwo->next;
}
return unionLL;
}
//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *LLOne, struct node *LLTwo)
{
intersectionLL = NULL;
//Search every element of LLOne in LLTwo, If found then add it to intersection List
while(LLOne != NULL)
{
if(search(LLTwo, LLOne->data))
{
insert(&intersectionLL, LLOne->data);
}
LLOne = LLOne->next;
}
return intersectionLL;
}
//Prints a linked list from head node till tail node
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
}
int main()
{
int i, LLOneCount, LLTwoCount, temp;
initialize();
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &LLOneCount);
printf("Enter %d integers\n", LLOneCount);
for(i=0; i<LLOneCount; i++)
{
scanf("%d", &temp);
insert(&LLOne, temp);
}
printLinkedList(LLOne);
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &LLTwoCount);
printf("Enter %d integers\n", LLTwoCount);
for(i=0; i<LLTwoCount; i++)
{
scanf("%d", &temp);
insert(&LLTwo, temp);
}
printLinkedList(LLTwo);
//printing Union of two given linked list
findunion(LLOne, LLTwo);
intersection(LLOne, LLTwo);
printf("\nUnion Linked List\n");
printLinkedList(unionLL);
printf("\nIntersection Linked List\n");
printLinkedList(intersectionLL);
return 0;
}