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

�

typedef struct Node {

int data;

struct Node* next;

} Node;

�

Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

return newNode;

}

�

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

Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

}

temp->next = newNode;

}

}

�

void printList(Node* head) {

Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

}

printf("\n");

}

�

void intersection(Node* list1, Node* list2) {

while (list1 != NULL && list2 != NULL) {

if (list1->data < list2->data) {

list1 = list1->next;

} else if (list1->data > list2->data) {

list2 = list2->next;

} else {

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

list1 = list1->next;

list2 = list2->next;

}

}

printf("\n");

}

�

void unionSet(Node* list1, Node* list2) {

while (list1 != NULL || list2 != NULL) {

if (list1 == NULL) {

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

list2 = list2->next;

} else if (list2 == NULL) {

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

list1 = list1->next;

} else {

if (list1->data < list2->data) {

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

list1 = list1->next;

} else if (list1->data > list2->data) {

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

list2 = list2->next;

} else {

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

list1 = list1->next;

list2 = list2->next;

}

}

}

printf("\n");

}

�

int main() {

Node* list1 = NULL;

Node* list2 = NULL;

�

int num, data;

�

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

scanf("%d", &num);

printf("Enter %d elements for the first list: ", num);

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

scanf("%d", &data);

insertNode(&list1, data);

}

�

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

scanf("%d", &num);

printf("Enter %d elements for the second list: ", num);

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

scanf("%d", &data);

insertNode(&list2, data);

}

�

printf("First List: ");

printList(list1);

printf("Second List: ");

printList(list2);

�

printf("Intersection: ");

intersection(list1, list2);

�

printf("Union: ");

unionSet(list1, list2);

�

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.