Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Project:- Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
In this project we have to create the inteface for Mathematical Set Theory, first we have to define the structure node and than we have to craete two linked list.
Then, the data of both linked list has to be added in one list i.e Union list.
Then the data which is same has be added in another list called Intersection list.
We will create the Structure Node
struct node
{
int data;
struct node *next;
} *List_One, *List_Two, *union_List, *intersection_List;
void initialize()
{
List_One = List_Two = NULL; // initialising the linked list as 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;
}
Create Two Linked list
int main()
{
int i,C1, C2, temp;
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &C1);
printf("Enter %d integers\n", C1);
for(i=0; i<C1; i++)
{
scanf("%d", &temp);
insert(&List_One, temp);
}
printLinkedList(List_One);
// careting Second like list
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &C2);
printf("Enter %d integers\n", C2);
for(i=0; i<C2; i++)
{
scanf("%d", &temp);
insert(&List_Two, temp);
}
printLinkedList(List_Two);
Union of data of two list
struct node* findunion(struct node *List_One, struct node *List_Two)
{
union_List = NULL;
// Add all nodes of first Linked List to unionLL
struct node *temp = List_One;
while(temp != NULL)
{
insert(&union_List, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(List_Two != NULL)
{
if(!search(List_One, List_Two->data))
{
insert(&union_List, List_Two->data);
}
List_Two = List_Two->next;
}
return union_List;
}
Intersection of data of two list
struct node* intersection(struct node *List_One, struct node *List_Two)
{
intersection_List = NULL;
//Search every element of List_One in List_Two, If found then add it to intersection List
while(List_One != NULL)
{
if(search(List_Two, List_One->data))
{
insert(&intersection_List, List_One->data);
}
List_One = List_One->next;
}
return intersection_List;
Full Program:-
#include <stdio.h>
#include <stdlib.h>
// A structure of linked list node
struct node
{
int data;
struct node *next;
} *List_One, *List_Two, *union_List, *intersection_List;
void initialize()
{
List_One = List_Two = NULL; // initialising the linked list as 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 *List_One, struct node *List_Two)
{
union_List = NULL;
// Add all nodes of first Linked List to unionLL
struct node *temp = List_One;
while(temp != NULL)
{
insert(&union_List, temp->data);
temp = temp->next;
}
// Insert those nodes of LLTwo which is not present in LLOne
while(List_Two != NULL)
{
if(!search(List_One, List_Two->data))
{
insert(&union_List, List_Two->data);
}
List_Two = List_Two->next;
}
return union_List;
}
//Returns the Linked List which contains common nodes of two given linked list
struct node* intersection(struct node *List_One, struct node *List_Two)
{
intersection_List = NULL;
//Search every element of List_One in List_Two, If found then add it to intersection List
while(List_One != NULL)
{
if(search(List_Two, List_One->data))
{
insert(&intersection_List, List_One->data);
}
List_One = List_One->next;
}
return intersection_List;
}
//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,C1, C2, temp;
//Creating First linked List
printf("Enter number of nodes in first Linked List\n");
scanf("%d", &C1);
printf("Enter %d integers\n", C1);
for(i=0; i<C1; i++)
{
scanf("%d", &temp);
insert(&List_One, temp);
}
printLinkedList(List_One);
// careting Second like list
printf("\nEnter number of nodes in second Linked List\n");
scanf("%d", &C2);
printf("Enter %d integers\n", C2);
for(i=0; i<C2; i++)
{
scanf("%d", &temp);
insert(&List_Two, temp);
}
printLinkedList(List_Two);
//printing Union of two given linked list
findunion(List_One, List_Two);
intersection(List_One, List_Two);
printf("\nUnion Linked List\n");
printLinkedList(union_List);
printf("\nIntersection Linked List\n");
printLinkedList(intersection_List);
return 0;
}
Output:-
Enter 3 integers
12
23
15
15-->23-->12
Enter number of nodes in second Linked List
3
Enter 3 integers
12
45
62
62-->45-->12
Union Linked List
45-->62-->12-->23-->15
Intersection Linked List
12





