Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
// Structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Function to insert a new node at the end of the linked list
void insertNode(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to print the elements of the linked list
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to compute the intersection of two linked lists
struct Node* setIntersection(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
struct Node* temp1 = list1;
while (temp1 != NULL) {
struct Node* temp2 = list2;
while (temp2 != NULL) {
if (temp1->data == temp2->data) {
insertNode(&result, temp1->data);
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
return result;
}
// Function to compute the union of two linked lists
struct Node* setUnion(struct Node* list1, struct Node* list2) {
struct Node* result = NULL;
struct Node* temp1 = list1;
while (temp1 != NULL) {
insertNode(&result, temp1->data);
temp1 = temp1->next;
}
struct Node* temp2 = list2;
while (temp2 != NULL) {
insertNode(&result, temp2->data);
temp2 = temp2->next;
}
return result;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
// Inserting elements into the first list
insertNode(&list1, 1);
insertNode(&list1, 2);
insertNode(&list1, 3);
// Inserting elements into the second list
insertNode(&list2, 2);
insertNode(&list2, 3);
insertNode(&list2, 4);
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
// Computing the intersection of the two lists
struct Node* intersection = setIntersection(list1, list2);
printf("Intersection: ");
printList(intersection);
// Computing the union of the two lists
struct Node* unionSet = setUnion(list1, list2);
printf("Union: ");
printList(unionSet);
return 0;
}

-
We start by including the necessary header files:
stdio.hfor input/output operations andstdlib.hfor dynamic memory allocation. -
We define a structure called
Nodethat represents a node in the linked list. It has two members:datato store the data value andnextto point to the next node in the list. -
The
createNodefunction is used to create a new node and initialize its data and next pointer. It takes a value as input and returns a pointer to the newly created node. -
The
insertNodefunction is responsible for inserting a new node at the end of the linked list. It takes a double pointer to the head of the list and a value to be inserted. If the list is empty, it creates a new node and assigns it as the head. Otherwise, it traverses to the end of the list and adds the new node there. -
The
printListfunction is used to print the elements of the linked list. It takes a pointer to the head of the list and iterates through the list, printing each node's data value. -
The
setIntersectionfunction computes the intersection of two linked lists. It takes pointers to the heads of the two lists as input and returns a new linked list representing the intersection. It traverses through the first list and for each node, it traverses through the second list to find any matching elements. If a match is found, it inserts that element into the result list. -
The
setUnionfunction computes the union of two linked lists. It takes pointers to the heads of the two lists as input and returns a new linked list representing the union. It traverses through the first list and inserts each element into the result list. Then, it traverses through the second list and inserts any non-duplicate elements into the result list. -
In the
mainfunction, we create two empty linked lists:list1andlist2. -
We then insert elements into
list1andlist2using theinsertNodefunction. -
We print the contents of
list1andlist2using theprintListfunction. -
We compute the intersection of
list1andlist2by calling thesetIntersectionfunction and store the result in theintersectionvariable. -
We print the intersection by calling the
printListfunction withintersectionas the argument. -
We compute the union of
list1andlist2by calling thesetUnionfunction and store the result in theunionSetvariable. -
We print the union by calling the
printListfunction withunionSetas the argument. -
Finally, the program ends with a
return 0statement.