Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to insert a new node at the end of a linked list
void insert(Node** head, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to display the linked list
void display(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void createnode(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 of the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
notnull(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL) {
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}
// Function to find the intersection of two linked lists
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) {
if (notnull(head2, t1->data))
createnode(&result, t1->data);
t1 = t1->next;
}
return result;
}
// Function to get the union of two linked lists
Node* getUnion(Node* list1, Node* list2) {
Node* unionList = NULL;
Node* temp1 = list1;
Node* temp2 = list2;
// Insert all nodes of the first list into the union list
while (temp1 != NULL) {
insert(&unionList, temp1->data);
temp1 = temp1->next;
}
// Insert nodes of the second list into the union list if they don't already exist
while (temp2 != NULL) {
Node* temp = unionList;
int isDuplicate = 0;
while (temp != NULL) {
if (temp->data == temp2->data) {
isDuplicate = 1;
break;
}
temp = temp->next;
}
if (!isDuplicate) {
insert(&unionList, temp2->data);
}
temp2 = temp2->next;
}
return unionList;
}
int main() {
Node* list1 = NULL;
Node* list2 = NULL;
int n1, n2, data;
printf("Enter the number of elements for the first linked list: ");
fflush(stdout);
scanf("%d", &n1);
printf("Enter the elements of the first linked list: ");
for (int i = 0; i < n1; i++) {
fflush(stdout);
scanf("%d", &data);
insert(&list1, data);
}
printf("Enter the number of elements for the second linked list: ");
fflush(stdout);
scanf("%d", &n2);
printf("Enter the elements of the second linked list: ");
for (int i = 0; i < n2; i++) {
fflush(stdout);
scanf("%d", &data);
insert(&list2, data);
}
printf("First linked list: ");
display(list1);
printf("Second linked list: ");
display(list2);
Node* intersection = getIntersection(list1, list2);
printf("Intersection of the two linked lists: ");
display(intersection);
// Get the union of the two linked lists
Node* unionList = getUnion(list1, list2);
printf("Union of the two linked lists: ");
display(unionList);
return 0;
}
Output:
