Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Aim :-
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, 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
Theory :-
Union – All the elements that are in at least one of the two lists.
Intersection – Only the elements which are present in both lists.
Linked List :-
- A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores the data and the address of the next node. For example,

- You have to start somewhere, so we give the address of the first node a special name called HEAD. Also, the last node in the linked list can be identified because its next portion points to NULL.
Representation of Linked List
- Let's see how each node of the linked list is represented. Each node consists:
-
- A data item
- An address of another node
We wrap both the data item and the next node reference in a struct as:
struct node { int data; struct node *next; };There are three common types of Linked List.
------------------------------------------------------------------------------------------
Code :-
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void push(struct Node** head_ref,
int new_data);
bool isPresent(struct Node* head,
int data);
//Function to get union of two linked lists head1 and head2
struct Node* getUnion(struct Node* head1,
struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
// Insert all elements of list1 to the result list
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
push(&result, t2->data);
t2 = t2->next;
}
return result;
}
// Function to get intersection of two linked lists head1 and head2 */
struct Node* getIntersection(struct Node* head1,
struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
push(&result, t1->data);
t1 = t1->next;
}
return result;
}
void push(struct Node** head_ref,
int new_data)
{
// Allocate node
struct Node* new_node =
(struct Node*)malloc(
sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
bool isPresent(struct Node* head,
int data)
{
struct Node* t = head;
while (t != NULL)
{
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}
int main()
{
// Start with the empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
push(&head1, 10);
push(&head1, 40);
push(&head1, 15);
push(&head1, 20);
push(&head2, 10);
push(&head2, 20);
push(&head2, 40);
push(&head2, 80);
intersecn = getIntersection(head1,
head2);
unin = getUnion(head1, head2);
printf("First list is \n");
printList(head1);
printf("\nSecond list is \n");
printList(head2);
printf("\nIntersection list is \n");
printList(intersecn);
printf("\nUnion list is \n");
printList(unin);
return 0;
}
Output :-
