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>

// Node structure for the linked lists

struct Node {

int data;

struct Node *next;

};

// Function to traverse and print a linked list

void linkedListTraversal(struct Node *ptr) {

while (ptr != NULL) {

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

ptr = ptr->next;

}

printf("\n");

}

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

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

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

ptr->data = data;

ptr->next = NULL;

if (head == NULL) {

return ptr;

}

struct Node *p = head;

while (p->next != NULL) {

p = p->next;

}

p->next = ptr;

return head;

}

// Function to find the union of two sets

struct Node* findUnion(struct Node *head1, struct Node *head2) {

struct Node *unionHead = NULL;

struct Node *temp1 = head1;

// Insert all elements of the first list into the union list

while (temp1 != NULL) {

unionHead = insertAtEnd(unionHead, temp1->data);

temp1 = temp1->next;

}

// Insert elements of the second list if they are not already in the union list

struct Node *temp2 = head2;

while (temp2 != NULL) {

struct Node *search = unionHead;

int found = 0;

while (search != NULL) {

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

found = 1;

break;

}

search = search->next;

}

if (!found) {

unionHead = insertAtEnd(unionHead, temp2->data);

}

temp2 = temp2->next;

}

return unionHead;

}

// Function to find the intersection of two sets

struct Node* findIntersection(struct Node *head1, struct Node *head2) {

struct Node *intersectionHead = NULL;

struct Node *temp1 = head1;

// Check elements of the first list against the second list

while (temp1 != NULL) {

struct Node *temp2 = head2;

while (temp2 != NULL) {

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

intersectionHead = insertAtEnd(intersectionHead, temp1->data);

break;

}

temp2 = temp2->next;

}

temp1 = temp1->next;

}

return intersectionHead;

}

int main() {

struct Node *head1 = NULL;

struct Node *head2 = NULL;

// Create first linked list: 1 -> 2 -> 3

head1 = insertAtEnd(head1, 1);

head1 = insertAtEnd(head1, 2);

head1 = insertAtEnd(head1, 3);

// Create second linked list: 3 -> 4 -> 5

head2 = insertAtEnd(head2, 3);

head2 = insertAtEnd(head2, 4);

head2 = insertAtEnd(head2, 5);

printf("1st set Elements: ");

linkedListTraversal(head1);

printf("2nd set Elements: ");

linkedListTraversal(head2);

// Find and print the union of the two lists

struct Node *unionList = findUnion(head1, head2);

printf("Union: ");

linkedListTraversal(unionList);

// Find and print the intersection of the two lists

struct Node *intersectionList = findIntersection(head1, head2);

printf("Intersection: ");

linkedListTraversal(intersectionList);

return 0;

}

logic)

Data Structures: A single Node structure for simplicity.

Traversal Function: linkedListTraversal to traverse and print a linked list.

Insertion Function: insertAtEnd to add elements to the end of a linked list.

Union Function: findUnion combines elements from both linked lists without duplicates.

Intersection Function: findIntersection identifies common elements between the two linked lists.

Main Function: Initializes the linked lists, performs insertions, and prints the results, including the union and intersection of the two lists.

output)

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.