Challenge

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>

// Define a structure for a node in the linked list

struct Node {

int data;

struct Node* next;

};

// Function to create a new node with the given data

struct Node* createNode(int data) {

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

if (newNode == NULL) {

printf("Memory allocation failed.\n");

exit(1);

}

newNode->data = data;

newNode->next = NULL;

return newNode;

}

// Function to insert an element at the end of a linked list

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

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

struct Node* current = *head;

while (current->next != NULL) {

current = current->next;

}

current->next = newNode;

}

}

// Function to display the elements of a linked list

void displayList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

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

current = current->next;

}

printf("NULL\n");

}

// Function to compute the union of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL) {

insertEnd(&result, list1->data);

list1 = list1->next;

}

while (list2 != NULL) {

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

insertEnd(&result, list2->data);

}

list2 = list2->next;

}

return result;

}

// Function to check if an element is in the list

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

struct Node* current = head;

while (current != NULL) {

if (current->data == data) {

return 1; // Element found in the list

}

current = current->next;

}

return 0; // Element not found in the list

}

// Function to compute the intersection of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL) {

if (isInList(list2, list1->data)) {

insertEnd(&result, list1->data);

}

list1 = list1->next;

}

return result;

}

int main() {

struct Node* list1 = NULL;

struct Node* list2 = NULL;

int n, m;

printf("Enter the number of elements in the first list: ");

scanf("%d", &n);

printf("Enter the elements of the first list:\n");

for (int i = 0; i < n; i++) {

int data;

scanf("%d", &data);

insertEnd(&list1, data);

}

printf("Enter the number of elements in the second list: ");

scanf("%d", &m);

printf("Enter the elements of the second list:\n");

for (int i = 0; i < m; i++) {

int data;

scanf("%d", &data);

insertEnd(&list2, data);

}

printf("List 1: ");

displayList(list1);

printf("List 2: ");

displayList(list2);

struct Node* unionResult = setUnion(list1, list2);

printf("Union of the two lists: ");

displayList(unionResult);

struct Node* intersectionResult = setIntersection(list1, list2);

printf("Intersection of the two lists: ");

displayList(intersectionResult);

// Free memory

while (list1 != NULL) {

struct Node* temp = list1;

list1 = list1->next;

free(temp);

}

while (list2 != NULL) {

struct Node* temp = list2;

list2 = list2->next;

free(temp);

}

while (unionResult != NULL) {

struct Node* temp = unionResult;

unionResult = unionResult->next;

free(temp);

}

while (intersectionResult != NULL) {

struct Node* temp = intersectionResult;

intersectionResult = intersectionResult->next;

free(temp);

}

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.