Challenge

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

Project 1:-

/*

* proj.c

*

* Created on: Dec 29, 2023

* Author: user

*/

#include

#include

// Node structure for linked list

struct Node {

int data;

struct Node* next;

};

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

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

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

newNode->data = data;

newNode->next = NULL;

if (*head == NULL) {

*head = newNode;

} else {

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

}

temp->next = newNode;

}

}

// Function to print the elements of a linked list

void printList(struct Node* head) {

while (head != NULL) {

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

fflush(stdout);

head = head->next;

}

printf("\n");fflush(stdout);

}

// Function to find the intersection of two linked lists

struct Node* findIntersection(struct Node* listA, struct Node* listB) {

struct Node* result = NULL;

while (listA != NULL && listB != NULL) {

if (listA->data == listB->data) {

insertNode(&result, listA->data);

listA = listA->next;

listB = listB->next;

} else if (listA->data < listB->data) {

listA = listA->next;

} else {

listB = listB->next;

}

}

return result;

}

// Function to find the union of two linked lists

struct Node* findUnion(struct Node* listA, struct Node* listB) {

struct Node* result = NULL;

while (listA != NULL || listB != NULL) {

if (listA == NULL) {

insertNode(&result, listB->data);

listB = listB->next;

} else if (listB == NULL) {

insertNode(&result, listA->data);

listA = listA->next;

} else {

if (listA->data < listB->data) {

insertNode(&result, listA->data);

listA = listA->next;

} else if (listA->data > listB->data) {

insertNode(&result, listB->data);

listB = listB->next;

} else {

insertNode(&result, listA->data);

listA = listA->next;

listB = listB->next;

}

}

}

return result;

}

// Function to create a linked list based on user input

struct Node* createListFromUserInput() {

struct Node* head = NULL;

int n, data;

printf("Enter the number of elements: ");

fflush(stdout);

scanf("%d", &n);

fflush(stdout);

printf("Enter the elements:\n");fflush(stdout);

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

scanf("%d", &data);fflush(stdout);

insertNode(&head, data);

}

return head;

}

int main() {

// Create linked lists based on user input

struct Node* listA = createListFromUserInput();

struct Node* listB = createListFromUserInput();

// Print the input lists

printf("List A: ");fflush(stdout);

printList(listA);

printf("List B: ");fflush(stdout);

printList(listB);

// Find and Print the intersection of the lists

struct Node* intersection = findIntersection(listA, listB);

printf("Intersection: ");

printList(intersection);

// Find and print the union of the lists

struct Node* unionResult = findUnion(listA, listB);

printf("Union: ");

printList(unionResult);

// Free memory used by linked lists

free(intersection);

free(unionResult);

return 0;

}

Output:-

Enter the number of elements: 5

Enter the elements:

0 1 2 3 4

Enter the number of elements: 6

Enter the elements:

2 3 4 5 6 7

List A: 0 1 2 3 4

List B: 2 3 4 5 6 7

Intersection: 2 3 4

Union: 0 1 2 3 4 5 6 7

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.