Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Que.Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Ans.
Math oparetion Union:
In the mathematics set theory there are two set set A and set B ,now meaning of union means the eliment which set of elimants for both are called union set.
like,
A=[4,5,6,8]
B=[5,6,9,2]
now Union set =[4,5,6,9,2]
Math oparetion Intersection:
In the mathematical set theory there are two set A and B , Now meaning of Inter section is set of common eliment between A and B.
like,
A=[4,5,6,8]
B=[5,6,9,2]
now Intersection set =[5,6]
Program:
#include
#include
typedef struct node //Defining the structure
{
int data;//creating data type of variable
struct node* next;//creating self referential node
}
Node;//changing the structure name as node
// Declare variables for different linked lists
static Node* head1, *head2, *head_union, *head_intersection, *newnode, *tail, *res_union, *res_intersection;
// Function to insert a node at the beginning of a linked list
Node* insert_node(Node* head_ref, int n);
// Function to display the elements of a linked list
void display_list(Node* head_ref);
// Function to check if a given value exists in a linked list
int compare(Node* head, int data);
// Function to get the union of two linked lists
Node* get_union(Node* head_Pointer1, Node* head_Pointer2);
// Function to get the intersection of two linked lists
Node* get_intersection(Node* head_Pointer1, Node* head_Pointer2);
// Function to display the elements of a linked list
void display_list(Node* head_ref) {
if (head_ref == NULL) {
printf("List is empty.\n");
} else {
tail = head_ref;
while (tail != NULL) {
printf("%d ", tail->data);
tail = tail->next;
}
}
}
// Function to insert a node at the beginning of a linked list
Node* insert_node(Node* head_ref, int n) {
printf("\nInserting Elements at the beginning...\n");
newnode = (Node*)malloc(sizeof(Node));//allocating memory to the node
printf("Address of created newnode: %p\n", newnode);
newnode->data = n;//assigning values to the node
printf("Inserted Value is: %d\n", newnode->data);
newnode->next = head_ref;//address of the previous address is transferring to the new node
head_ref = newnode;//transferring new address to head_ref for linking new node
return head_ref;
}
// Function to compare a value with the elements of a linked list
int compare(Node* head, int data) {
while (head != NULL) {
if (head->data == data)
return 1;
head = head->next;
}
return 0;
}
// Function to get the union of two linked lists
Node* get_union(Node* head_Pointer1, Node* head_Pointer2) {
while (head_Pointer1 != NULL) {
head_union = insert_node(head_union, head_Pointer1->data);
head_Pointer1 = head_Pointer1->next;
}
while (head_Pointer2 != NULL) {
if (!compare(head_union, head_Pointer2->data))
head_union = insert_node(head_union, head_Pointer2->data);
head_Pointer2 = head_Pointer2->next;
}
return head_union;
}
// Function to get the intersection of two linked lists
Node* get_intersection(Node* head_Pointer1, Node* head_Pointer2) {
while (head_Pointer1 != NULL) {
if (compare(head_Pointer2, head_Pointer1->data))
head_intersection = insert_node(head_intersection, head_Pointer1->data);
head_Pointer1 = head_Pointer1->next;
}
return head_intersection;
}
int main() {
int first_list_nodes = 0, second_list_nodes = 0, first_list_items = 0, second_list_items = 0;
int f;
// Initialize the linked list heads to NULL
head1 = NULL;
head2 = NULL;
// Get input for the first linked list
printf("Enter the number of nodes in the first list: ");
fflush(stdout);
scanf("%d", &first_list_nodes);
printf("Enter %d elements: ", first_list_nodes);
for (f = 0; f < first_list_nodes; f++) {
fflush(stdout);
scanf("%d", &first_list_items);
head1 = insert_node(head1, first_list_items);
}
display_list(head1);
// Get input for the second linked list
printf("\nEnter the number of nodes in the second list: ");
fflush(stdout);
scanf("%d", &second_list_nodes);
printf("Enter %d elements: ", second_list_nodes);
for (f = 0; f < second_list_nodes; f++) {
fflush(stdout);
scanf("%d", &second_list_items);
head2 = insert_node(head2, second_list_items);
}
display_list(head2);
// Get the union of the two linked lists
res_union = get_union(head1, head2);
printf("\nHere is the result of Union: ");
display_list(res_union);
// Get the intersection of the two linked lists
res_intersection = get_intersection(head1, head2);
printf("\nHere is the result of Intersection: ");
display_list(res_intersection);
return 0;
}
Output:


