Challenge

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

CODE 

#include

#include

 

// Define a 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 data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

}

 

// Function to insert a node at the end of a linked list

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

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

}

temp->next = newNode;

}

}

 

// Function to find the intersection of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL) {

struct Node* temp = list2;

while (temp != NULL) {

if (list1->data == temp->data) {

insertNode(&result, list1->data);

break;

}

temp = temp->next;

}

list1 = list1->next;

}

return result;

}

 

// Function to find the union of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL) {

insertNode(&result, list1->data);

list1 = list1->next;

}

while (list2 != NULL) {

if (!contains(result, list2->data)) {

insertNode(&result, list2->data);

}

list2 = list2->next;

}

return result;

}

 

// Function to check if a linked list contains a specific value

int contains(struct Node* head, int data) {

struct Node* current = head;

while (current != NULL) {

if (current->data == data) {

return 1;

}

current = current->next;

}

return 0;

}

 

// Function to print a linked list

void printList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

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

current = current->next;

}

printf("\n");

}

 

int main() {

struct Node* list1 = NULL;

struct Node* list2 = NULL;

struct Node* result_intersection = NULL;

struct Node* result_union = NULL;

 

// Insert elements into list1

insertNode(&list1, 1);

insertNode(&list1, 2);

insertNode(&list1, 3);

 

// Insert elements into list2

insertNode(&list2, 2);

insertNode(&list2, 3);

insertNode(&list2, 4);

 

printf("List 1: ");

printList(list1);

printf("List 2: ");

printList(list2);

 

result_intersection = intersection(list1, list2);

result_union = unionSet(list1, list2);

 

printf("Intersection: ");

printList(result_intersection);

 

printf("Union: ");

printList(result_union);

 

// Free allocated memory

free(result_intersection);

free(result_union);

 

return 0;

}

 

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.