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 :

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

Ans : -

  • Creating two list of data using linked list method.
  • Insert the node to 1st and 2nd list accordingly.
  • Intersection of data of two lists.
  • Getting Union of two list.

#include<stdio.h>
#include<stdlib.h>
// creating link list node
struct Node {
int data;
struct Node* next;
}; // struct node defines single node in linked list 'next ' and 'head ' defines pointer

struct List {
struct Node* head;
};
// declaring function

struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//

void insertNode(struct List* list, int data) {
struct Node* newNode = createNode(data);
if (list->head == NULL) {
list->head = newNode;
} else {
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// function to print a linked list

void displayList(struct List* list) {
struct Node* current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// function to get intersection of two linked list

struct List findIntersection(const struct List* list1, const struct List* list2) {
struct List intersection;
intersection.head = NULL;
struct Node* current1 = list1->head;

while (current1 != NULL) {
struct Node* current2 = list2->head;
while (current2 != NULL) {
if (current1->data == current2->data) {
insertNode(&intersection, current1->data);
break;
}
current2 = current2->next;
}
current1 = current1->next;
}
return intersection;
}
// functin to get union of two list
struct List findUnion(const struct List* list1, const struct List* list2) {
struct List unionSet;
unionSet.head = NULL;
struct Node* current1 = list1->head;

while (current1 != NULL) {
insertNode(&unionSet, current1->data);
current1 = current1->next;
}

struct Node* current2 = list2->head;
while (current2 != NULL) {
if (!contains(&unionSet, current2->data)) {
insertNode(&unionSet, current2->data);
}
current2 = current2->next;
}
return unionSet;
}

int contains(const struct List* list, int data) {
struct Node* current = list->head;
while (current != NULL) {
if (current->data == data) {
return 1;
}
current = current->next;
}
return 0;
}
// main program to test the above functin

int main() {
struct List list1, list2;
list1.head = NULL;
list2.head = NULL;

insertNode(&list1, 1);
insertNode(&list1, 2);
insertNode(&list1, 3);
insertNode(&list1, 4);
insertNode(&list1, 5);

insertNode(&list2, 3);
insertNode(&list2, 4);
insertNode(&list2, 5);
insertNode(&list2, 6);
insertNode(&list2, 7);

printf("List 1: ");
displayList(&list1);

printf("List 2: ");
displayList(&list2);

struct List intersection = findIntersection(&list1, &list2);
printf("Intersection: ");
displayList(&intersection);

struct List unionSet = findUnion(&list1, &list2);
printf("Union: ");
displayList(&unionSet);

return 0;
}

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.