Challenge

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

/* Author: Kishore

Date: 25-12-24

*/

//Program code

// Node in singly linked list structure

struct Node { //Each node contains data and next node address

int data; // Data

struct Node* next; //next pointer

};

// Function to create a new node

struct Node* createNode(int data)

{

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

newNode->data = data; //initializes the value with given 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) //if head is empty new node become head

{

*head = newNode;

} else

{

struct Node* current = *head;

while (current->next != NULL) //check if the current node is empty or not

{

current = current->next; //if not travel to next list

}

current->next = newNode;

}

}

// Function to find the union of two lists

struct Node* getUnion(struct Node* head1, struct Node* head2)

{

struct Node* result = NULL;

struct Node* current = head1;

// Insert all elements from the first list

while (current != NULL)

{

insertNode(&result, current->data);

current = current->next;

}

// Insert elements from the second list that are not in the first list

current = head2;

while (current != NULL)

{

struct Node* temp = head1;

int found = 0;

while (temp != NULL)

{

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

{

found = 1;

break;

}

temp = temp->next;

}

if (!found)

{

insertNode(&result, current->data);

}

current = current->next;

}

return result;

}

// Function to find the intersection of two lists

struct Node* getIntersection(struct Node* head1, struct Node* head2)

{

struct Node* result = NULL;

struct Node* current = head1;

// Insert elements present in both lists

while (current != NULL)

{

struct Node* temp = head2;

while (temp != NULL)

{

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

{

insertNode(&result, current->data);

break;

}

temp = temp->next;

}

current = current->next;

}

return result;

}

// Function to print the linked list

void printList(struct Node* head)

{

struct Node* current = head;

while (current != NULL)

{

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

current = current->next;

}

printf("\n");

}

// Main function

int main()

{

struct Node* list1 = NULL;

struct Node* list2 = NULL;

int n, data;

// Input first list

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

scanf("%d", &n);

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

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

scanf("%d", &data);

insertNode(&list1, data);

}

// Input second list

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

scanf("%d", &n);

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

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

scanf("%d", &data);

insertNode(&list2, data);

}

// Compute union and intersection

struct Node* unionList = getUnion(list1, list2);

struct Node* intersectionList = getIntersection(list1, list2);

// Print results

printf("Union of the two lists: ");

printList(unionList);

printf("Intersection of the two lists: ");

printList(intersectionList);

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.