Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data);
bool isPresent(struct Node* head, int data);
struct Node* getUnion(struct Node* head1,struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
while (t1 != NULL) {
fun(&result, t1->data);
t1 = t1->next;
}
while (t2 != NULL) {
if (!isPresent(result, t2->data))
fun(&result, t2->data);
t2 = t2->next;
}
return result;
}
/* Function to get intersection of
two linked lists head1 and head2 */
struct Node* getIntersection(struct Node* head1,struct Node* head2)
{
struct Node* result = NULL;
struct Node* t1 = head1;
while (t1 != NULL)
{
if (isPresent(head2, t1->data))
fun(&result, t1->data);
t1 = t1->next;
}
return result;
}
void fun(struct Node** head_ref, int new_data)
{
struct Node* new_node= (struct Node*)malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* A utility function to print a linked list*/
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
bool isPresent(struct Node* head, int data)
{
struct Node* t = head;
while (t != NULL) {
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}
int main()
{
/* Start with the empty list */
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
fun(&list1, 20);
fun(&list1, 4);
fun(&list1, 15);
fun(&list1, 10);
fun(&list2, 10);
fun(&list2, 2);
fun(&list2, 4);
fun(&list2, 8);
intersecn = getIntersection(list1, list2);
unin = getUnion(list1, list2);
printf("\n First list is \n");
printList(list1);
printf("\n Second list is \n");
printList(list2);
printf("\n Intersection list is \n");
printList(intersecn);
printf("\n Union list is \n");
printList(unin);
return 0;
}