Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Project: Mathematical Set Theory Implementation in C

This project implements interfaces for Set Theory using Linked Lists in C. It allows users to input two lists of data and calculates the Union and Intersection of the two sets.

---

📌 Goals

1. Implement Set Theory operations (Union & Intersection) using Linked Lists in C.

2. Take two lists as input and compute their Union and Intersection.

---

🔹 Key Highlights

✅ Creating two lists dynamically using a Linked List.

✅ Inserting nodes into both lists.

✅ Computing the Intersection (common elements) of both lists.

✅ Computing the Union (all unique elements) of both lists.

---

📜 Deliverables

📌 Function to compute the Union of two linked lists.

📌 Function to compute the Intersection of two linked lists.

---

🚀 Expected Outcomes

🔹 Understanding how to create and manipulate Linked Lists.

🔹 Learning Set Theory operations using data structures.

🔹 Efficiently comparing elements of two lists.

---

📝 C Program for Set Operations using Linked List

#include <stdio.h>

#include <stdlib.h>

// Define Node structure

struct Node {

int data;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

}

// Function to insert a node into a linked list (if it does not exist)

void insertUnique(struct Node** head, int data) {

struct Node* temp = *head;

while (temp) {

if (temp->data == data) return; // Avoid duplicates

temp = temp->next;

}

struct Node* newNode = createNode(data);

newNode->next = *head;

*head = newNode;

}

// Function to print linked list

void printList(struct Node* head) {

while (head) {

printf("%d -> ", head->data);

head = head->next;

}

printf("NULL\n");

}

// Function to compute the Union of two linked lists

struct Node* getUnion(struct Node* list1, struct Node* list2) {

struct Node* result = NULL;

while (list1) {

insertUnique(&result, list1->data);

list1 = list1->next;

}

while (list2) {

insertUnique(&result, list2->data);

list2 = list2->next;

}

return result;

}

// Function to compute the Intersection of two linked lists

struct Node* getIntersection(struct Node* list1, struct Node* list2) {

struct Node* result = NULL;

struct Node* temp1 = list1;

while (temp1) {

struct Node* temp2 = list2;

while (temp2) {

if (temp1->data == temp2->data) {

insertUnique(&result, temp1->data);

break;

}

temp2 = temp2->next;

}

temp1 = temp1->next;

}

return result;

}

// Driver Code

int main() {

struct Node* list1 = NULL;

struct Node* list2 = NULL;

// Insert elements into first list

insertUnique(&list1, 1);

insertUnique(&list1, 3);

insertUnique(&list1, 5);

insertUnique(&list1, 7);

// Insert elements into second list

insertUnique(&list2, 2);

insertUnique(&list2, 3);

insertUnique(&list2, 5);

insertUnique(&list2, 8);

printf("List 1: ");

printList(list1);

printf("List 2: ");

printList(list2);

// Compute and print Union

struct Node* unionList = getUnion(list1, list2);

printf("\nUnion: ");

printList(unionList);

// Compute and print Intersection

struct Node* intersectionList = getIntersection(list1, list2);

printf("Intersection: ");

printList(intersectionList);

return 0;

}

---

📌 Example Output

List 1: 7 -> 5 -> 3 -> 1 -> NULL

List 2: 8 -> 5 -> 3 -> 2 -> NULL

Union: 8 -> 7 -> 5 -> 3 -> 2 -> 1 -> NULL

Intersection: 5 -> 3 -> NULL

---

💡 Key Learnings

✔ Dynamic memory allocation with linked lists.

✔ Efficient comparison of elements using nested loops.

✔ Avoiding duplicates w

hile inserting into lists.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.