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

struct Node {

int data;

struct Node* next;

};

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

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

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

if (newNode == NULL) {

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

exit(1);

}

newNode->data = value;

newNode->next = NULL;

if (*head == NULL) {

*head = newNode;

} else {

struct Node* current = *head;

while (current->next != NULL) {

current = current->next;

}

current->next = newNode;

}

}

// Function to display elements of a linked list

void displayList(struct Node* head) {

while (head != NULL) {

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

head = head->next;

}

printf("\n");

}

// Function to find the intersection of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL && list2 != NULL) {

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

insertNode(&result, list1->data);

list1 = list1->next;

list2 = list2->next;

} else if (list1->data < list2->data) {

list1 = list1->next;

} else {

list2 = list2->next;

}

}

return result;

}

// Function to find the union of two linked lists

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

struct Node* result = NULL;

while (list1 != NULL || list2 != NULL) {

if (list1 == NULL) {

insertNode(&result, list2->data);

list2 = list2->next;

} else if (list2 == NULL) {

insertNode(&result, list1->data);

list1 = list1->next;

} else if (list1->data < list2->data) {

insertNode(&result, list1->data);

list1 = list1->next;

} else if (list2->data < list1->data) {

insertNode(&result, list2->data);

list2 = list2->next;

} else {

insertNode(&result, list1->data);

list1 = list1->next;

list2 = list2->next;

}

}

return result;

}

// Function to free the memory allocated for a linked list

void freeList(struct Node** head) {

struct Node* current = *head;

struct Node* next;

while (current != NULL) {

next = current->next;

free(current);

current = next;

}

*head = NULL;

}

int main() {

struct Node* list1 = NULL;

struct Node* list2 = NULL;

// Input first list

int numElements1;

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

scanf("%d", &numElements1);

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

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

int value;

scanf("%d", &value);

insertNode(&list1, value);

}

// Input second list

int numElements2;

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

scanf("%d", &numElements2);

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

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

int value;

scanf("%d", &value);

insertNode(&list2, value);

}

// Find and display the intersection

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

printf("Intersection: ");

displayList(intersectionResult);

freeList(&intersectionResult);

// Find and display the union

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

printf("Union: ");

displayList(unionResult);

freeList(&unionResult);

// Free the memory allocated for the linked lists

freeList(&list1);

freeList(&list2);

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.