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 the 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 a node at the end of the list

void insertNode(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 print the elements of a linked list

void printList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

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

current = current->next;

}

printf("NULL\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) {

struct Node* current = list2;

while (current != NULL) {

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

insertNode(&result, list1->data);

break;

}

current = current->next;

}

list1 = list1->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) {

insertNode(&result, list1->data);

list1 = list1->next;

}

while (list2 != NULL) {

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

insertNode(&result, list2->data);

}

list2 = list2->next;

}

return result;

}

// Function to check if a value exists in a linked list

int findNode(struct Node* list, int value) {

struct Node* current = list;

while (current != NULL) {

if (current->data == value) {

return 1; // Value found

}

current = current->next;

}

return 0; // Value not found

}

int main() {

struct Node* list1 = NULL;

struct Node* list2 = NULL;

int n, m, value;

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

scanf("%d", &n);

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

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

scanf("%d", &value);

insertNode(&list1, value);

}

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

scanf("%d", &m);

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

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

scanf("%d", &value);

insertNode(&list2, value);

}

printf("First List: ");

printList(list1);

printf("Second List: ");

printList(list2);

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

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

printf("Intersection: ");

printList(intersectionResult);

printf("Union: ");

printList(unionResult);

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.