Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Project 1 :- Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List.
Objective:
The project focuses on implementing mathematical Set Theory interfaces in C programming, enabling users to input two lists of data and obtain their union and intersection. It involves creating linked lists for the data, inserting nodes accordingly, and computing the intersection and union of the lists. The deliverables include the intersection and union calculations, with expected outcomes of gaining knowledge in linked list node creation, set operations, and list element comparison. Overall, the project provides a practical exploration of set theory concepts through C programming.
Code:
/*
* Project_1.c
*
* Created on: 11-Feb-2024
* Author: Atul Futane
*/
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the list
void insertNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to print the elements of a list
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Function to find the intersection of two lists
void findIntersection(Node* list1, Node* list2) {
Node* temp1 = list1;
while (temp1 != NULL) {
Node* temp2 = list2;
while (temp2 != NULL) {
if (temp1->data == temp2->data) {
printf("%d ", temp1->data);
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
}
// Function to find the union of two lists
void findUnion(Node* list1, Node* list2) {
Node* temp1 = list1;
Node* temp2 = list2;
while (temp1 != NULL) {
printf("%d ", temp1->data);
temp1 = temp1->next;
}
while (temp2 != NULL) {
Node* current = list1;
int found = 0;
while (current != NULL) {
if (temp2->data == current->data) {
found = 1;
break;
}
current = current->next;
}
if (!found) {
printf("%d ", temp2->data);
}
temp2 = temp2->next;
}
}
int main() {
// Create and insert elements into the first list
Node* list1 = NULL;
insertNode(&list1, 1);
insertNode(&list1, 2);
insertNode(&list1, 3);
// Create and insert elements into the second list
Node* list2 = NULL;
insertNode(&list2, 2);
insertNode(&list2, 3);
insertNode(&list2, 4);
printf("List 1: ");
printList(list1);
printf("List 2: ");
printList(list2);
printf("Intersection: ");
findIntersection(list1, list2);
printf("\nUnion: ");
findUnion(list1, list2);
return 0;
}
Output:-
