Challenge

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

/*Union and intersection of 2 lists

date: 14/11/2023

Vishnu V Nair*/

#include <stdio.h>

#include <stdlib.h>

/** Linked list creation and insertion part **/

//Node structure for liked list

struct Node

{

int data;

struct Node* next;

};

//Function to insert new node

void insert_Node(struct Node** head, int data)

{

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

new_node->data= data;

new_node->next= NULL;

if (*head == NULL)

{

*head = new_node;

} //If list is empty, set head to new node

else

{

struct Node* current= *head;

while (current->next != NULL) //Transverse until lastnode is reached

{

current= current->next;

}

current->next = new_node;

}

}

//Function to print list elements

void print_list(struct Node* head)

{

while (head != NULL)

{

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

head = head->next;

} //Print the data in the current node and move to next node

}

/** To make union and check present data part **/

//Function to check if the element is already existing in the union list

int compare(struct Node* head, int data)

{

while(head != NULL)

{

if(head->data == data)

{

return 1;

} //Checks the current node data == data,if data found in list its true

head= head->next; //Move to next node to check

}

return 0; //To indicate the data is not found in the list

}

//Function to union 2 lists

void remove_duplicate(struct Node* list1, struct Node* list2)

{

struct Node* list= NULL; //List for union

while(list1 != NULL)

{

insert_Node(&list, list1->data);

list1= list1->next;

} //To save list1 data to union list

while(list2 != NULL)

{

if(!compare(list, list2->data))

{

insert_Node(&list, list2->data);

}

list2= list2->next;

} //Checking if list2 data is present in union list,if not add the data to union list

printf("\nUnion: ");

print_list(list); //To print union list

}

/* Intersection part */

//Function to intersect 2 lists

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

{

struct Node* list= NULL; //List for intersection

while(list1 != NULL)

{

struct Node* temp= list2; //List2 has been assigned to a temporary pointer 'temp'

while(temp != NULL)

{

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

{

insert_Node(&list, list1->data);

} //if list1 data == list2 data,then insert data to intersection list

temp= temp->next; //Jump to next node

} //Till list2 is not empty

list1= list1->next; //Jump to next node and repeat the loop

}

//To print Intersected list

printf("\nIntersection: ");

print_list(list);

}

/* Main Function */

int main()

{

//Setting empty lists

struct Node* set1= NULL;

struct Node* set2= NULL;

//Data insertion

insert_Node(&set1, 1);

insert_Node(&set1, 2);

insert_Node(&set1, 3);

insert_Node(&set1, 4);

insert_Node(&set1, 5);

insert_Node(&set2, 3);

insert_Node(&set2, 6);

insert_Node(&set2, 5);

insert_Node(&set2, 4);

//To print i/p list

printf("Set1: ");

print_list(set1);

printf("\nSet2: ");

print_list(set2);

//Function to print union of set1 & set2

remove_duplicate(set1, set2);

//Function to find and print intersection of set1 & set2

intersection(set1, set2);

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.