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.
Project explaination:-
Linked list: 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.

We 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.
Head Pointer:- Points the starting address of the list.
Tail pointer:- Temporary pointer mainly used for traversing the list.
New Node pointer:- Points the newest created node.
Program flow:-
1) Take input for Number of nodes in 1st Linked List.
2)Take a multiple input for data on nodes in 1st list using insert_element() function. Loops are used for multiple iteration.
3)Call display list function to display 1st list.
4)Repeat steps 1,2 & 3 for 2nd list.
5)Save the head pointer address returned by get_union() function pointer to a node type pointer res_union & repeat the same for getting intersection using different algorithm.
Implemenatation of function:-

Function declaration:-
1)insert_node():- Node*insert_node(Node*head_ref,intn);
Here, Node:-Structure name (to access structure created)
Node*head_ref:- Header pointer.
int n:- data.
2)display_list():- Void display_list(Node*head_ref);
Void:- No need to access the structure.
3)get_Union():- Node*get_union(Node*head_pointer1,Node*head_pointer2);
->Insert all the element of 1st list in Union list using head_pointer1.
->Now compare the union list element with 2nd list & if repetition is there insert that repeated element only one time.
->Return the head pointer for union list.

4)get_intersection():- Node*get_intersection(Node*head_pointer1,Node*head_pointer2);
5)Compare():- int compare(Node*head,int data);
OR
bool ispresent(Node*head,int data);
Program code:-
/*
* Project1.c
*MAthematical Set Theory
* Created on: 21-Mar-2023
* Author: Hp
*/
#include <stdio.h>//standard i/o header file
#include <stdlib.h>//header defines four variable types, several macros, and various functions for performing general functions.
#include <stdbool.h>//library for boolean operation of boolean data type.
/* Linked list node */
/*...........Define the node.................*/
struct Node
{
int data;
struct Node* next;//Self referential pointer(it will call its structure).
}Node;
/* function to insert a node at the beginning of a linked list*/
void insert(struct Node** head_ref, int new_data);
/* function to check if given data is present in a list */
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)//To check whether list is empty or not.
{
insert(&result, t1->data);
t1 = t1->next;
}
// Insert those elements of list2 which are not present in result list
while (t2 != NULL)//To check whether list is empty or not.
{
if (!isPresent(result, t2->data))
insert(&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;
/* Traverse list1 and search each element of it in list2.
If the element is present in list 2, then
insert the element to result*/
while (t1 != NULL) //To check whether list is empty or not.
{
if (isPresent(head2, t1->data))
insert(&result, t1->data);
t1 = t1->next;
}
return result;
}
/* Insert function used to insert a node at the beginning of a linked list*/
void insert(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* A display list function is used to print a linked list*/
void displaylist(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* isPresent function that returns true if data is present in linked list else return false */
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;
}
/* program to run above function*/
int main()
{
int a,b,c,d,s,t,u,v;
/* Start with the empty list */
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
printf("Enter the 4 elements for the 1st array:-");
//Asking the elements
fflush(stdout);
//input the elements by the user
scanf("\n%d %d %d %d",&a,&b,&c,&d);
/*create a linked lists with the input data */
insert(&head1, a);
insert(&head1, b);
insert(&head1, c);
insert(&head1, d);
printf("Enter the 4 elements for the 2nd array:-");
//Asking the elements
fflush(stdout);
//input the elements by the user
scanf("\n%d %d %d %d",&s,&t,&u,&v);
/*create a linked lists with the input data*/
insert(&head2, s);
insert(&head2, t);
insert(&head2, u);
insert(&head2, v);
intersecn = getIntersection(head1, head2);
unin = getUnion(head1, head2);
printf("\n First list is:-");
displaylist(head1);
fflush(stdout);
printf("\n Second list is:-");
displaylist(head2);
fflush(stdout);
printf("\n Intersection list is:-");
displaylist(intersecn);
fflush(stdout);
printf("\n Union list is:-");
displaylist(unin);
fflush(stdout);
return 0;
}
Observation/Result:
