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

#include

struct Node

{                                    // Structure for a node in the linked list

int data;

struct Node* next;

};

struct Node* createNode(int data)                 // Function to create a new node

{                                                   

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

newNode->data = data;

newNode->next = NULL;

return newNode;

}

void insertNode(struct Node** head, int data)                   // Function to insert a node at the end of the linked list

{

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;

}

}

void printList(struct Node* head)                          // Function to print the linked list

{                       

struct Node* current = head;

while (current != NULL)

{

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

current = current->next;

}

printf("\n");

}

struct Node* intersection(struct Node* list1, struct Node* list2)                     // Function to find the intersection of two linked lists

{                      

struct Node* result = NULL;

struct Node* current = list1;

while (current != NULL)

{

struct Node* temp = list2;

while (temp != NULL)

{

if (current->data == temp->data)

{

insertNode(&result, current->data);

break;

}

temp = temp->next;

}

current = current->next;

}

return result;

}

struct Node* unionSet(struct Node* list1, struct Node* list2)                   // Function to find the union of two linked lists

{                         

struct Node* result = NULL;

struct Node* current = list1;

while (current != NULL)

{

insertNode(&result, current->data);

current = current->next;

}

current = list2;

while (current != NULL)

{

if (!intersection(result, createNode(current->data)))

{

insertNode(&result, current->data);
}

current = current->next;

}

return result;

}

void freeList(struct Node* head)                            // Function to free the memory used by the linked list

{

struct Node* current = head;

while (current != NULL)

{

struct Node* temp = current;

current = current->next;

free(temp);

}

}

int main()

{

struct Node* list1 = NULL;

struct Node* list2 = NULL;

int n, data;                                           

printf("Enter the number of elements for List 1: ");        // Input data for list 1

fflush(stdout);

scanf("%d", &n);

printf("Enter �lements for List 1:\n", n);

fflush(stdout);

for (int i = 0; i < n>

{

scanf("%d", &data);

insertNode(&list1, data);

}

printf("Enter the number of elements for List 2: ");                    // Input data for list 2

fflush(stdout);

scanf("%d", &n);

printf("Enter �lements for List 2:\n", n);

fflush(stdout);

for (int i = 0; i < n>

{

scanf("%d", &data);

insertNode(&list2, data);

}

printf("List 1: ");           

fflush(stdout);

printList(list1);

printf("List 2: ");

fflush(stdout);

printList(list2);

struct Node* intersect = intersection(list1, list2);                     // Calculate and print the intersection of the two lists

printf("Intersection: ");

fflush(stdout);

printList(intersect);

struct Node* unionSetResult = unionSet(list1, list2);                   // Calculate and print the union of the two lists

printf("Union: ");

fflush(stdout);

printList(unionSetResult);

freeList(list1);                              // Free memory used by the linked lists

freeList(list2);

freeList(intersect);

freeList(unionSetResult);

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.