Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
Overview of the Object:
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.
The main objective of the project is to implement Mathematical set theory using linked list. The set operation has many number of operations. Here, we are going to perform union and intersection operations by creating two linked list. We are creating a structure it has a collection of different data. Here is the process to first create two lists using the reference address.
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.
Here, we have to give the two list of data as input to get the output as union and intersection.
Union- The Union of two sets is the set of elements which are in either set.
Intersection- The intersection is the common value of set A and set B.
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.
Deliverable of this project:
Union of two list.
List 1 ={1, 3, 5} & List 2 ={ 4, 5, 6, 3}
After performing union function, it should print : {4, 6, 1, 3, 5}
Intersection of two list.
List 1 ={1. 3. 5} & List 2 ={4, 5, 6, 3}
After performing intersection function, it should print : {3, 5}
#include
#include
/* A structure of linked list node*/
struct node
{
int data;
struct node *next;
} *List1, *List2, *UnionList, *IntersectionList;
void initialize()
{
List1 = List2 = 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 *List1, struct node *List2)
{
UnionList = NULL;
/*Add all nodes of first Linked List to unionLinkedList*/
struct node *temp = List1;
while(temp != NULL)
{
insert(&UnionList, temp->data);
temp = temp->next;
}
/*Insert those nodes of LLTwo which is not present in LinkedListOne*/
while(List2 != NULL)
{
if(!search(List1, List2->data))
{
insert(&UnionList, List2->data);
}
List2 = List2->next;
}
return UnionList;
}
/*Returns the Linked List which contains common nodes of two given linked list*/
struct node* Intersection(struct node *List1, struct node *List2)
{
IntersectionList = NULL;
/*Search every element of LLOne in LLTwo, If found then add it to intersection List*/
while(List1 != NULL)
{
if(search(List2, List1->data))
{
insert(&IntersectionList, List1->data);
}
List1 = List1->next;
}
return IntersectionList;
}
/*Prints a linked list from head node till tail node*/
void printLinkedList(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
fflush(stdout);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
fflush(stdout);
}
}
int main()
{
int i, List1Count, List2Count, temp;
/*Creating First linked List*/
printf("Enter number of nodes in first Linked List:\n");
fflush(stdout);
scanf("%d", &List1Count);
printf("Enter %d integers:\n", List1Count);
fflush(stdout);
for(i=0; i
Output:
