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.c
*
* Created on: Mar 8, 2023
* Author: Bhaskar G
*/
// C program to find union and intersection of two unsorted linked lists
#include
#include
#include
// Linked list node
struct Node
{
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data);
int isPresent(struct Node* head, int data);
// Function to get union of two linked lists head1 and head2
struct Node* getUnion(struct Node* head1, struct Node* head2)
{
struct Node* result = NULL;
struct Node *t1 = head1, *t2 = head2;
// Insert all elements of list to the result list
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
// Insert elements of list which are not present in result list
while (t2 != NULL)
{
if (!isPresent(result, t2->data))
push(&result, t2->data);
t2 = t2->next;
}
return result;
}
// Function to getintersection 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))
push(&result, t1->data);
t1 = t1->next;
}
return result;
}
void push(struct Node** head_ref, int new_data)
{
// allocate node
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
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;
}
void printList(struct Node* node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int 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()
{
// empty list
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersect = NULL;
struct Node* unin = NULL;
// create a linked lists for set1 16->17->5->1
push(&head1, 17);
push(&head1, 16);
push(&head1, 5);
push(&head1, 1);
// create a linked lists for set2 7->5->1->9
push(&head2, 1);
push(&head2, 5);
push(&head2, 7);
push(&head2, 9);
intersect= getIntersection(head1, head2);
unin = getUnion(head1, head2);
printf("\n First set is \n");
printList(head1);
printf("\n Second set is \n");
printList(head2);
printf("\n Intersection sets is \n");
printf("\n Union sets is \n");
printList(unin);
return 0;
}
Overview:-
This project involves writing a C program to find the union and intersection of two unsorted linked lists. The program contains the implementation of two functions, getUnion and getIntersection, which takes two linked lists as input and returns the union and intersection of the two linked lists respectively. The program also contains other utility functions such as push, isPresent and printList.
Goals (2 to 3 points):-
1.The goal of this program is to find the intersection and union of two unsorted linked lists.
2.The program uses a push function to add new data to the linked lists and an isPresent function to see if the data is present in the list.
3.The getUnion and getIntersection functions are used to find the union and intersection of the two lists.
Key Highlights of the Project: (4 to 5 point):-
1. The project implements the union and intersection of two linked list.
2. It uses push() which is used to insert a node in a linked list and isPresent() which checks if the data is present in the list or not.
3. It also uses printList() which is used to print the linked list.
4. Main() is used to test the project by creating two linked list and calling the getUnion() and getIntersection() functions.
Deliverables of this project: (4 to 5 points):-
Elimination of duplication of data in union set.
Printing of both the sets.
Printing of Intersection and Union of the sets.
Actual Outcomes:-
First set is
1 5 16 17
Second set is
9 7 5 1
Intersection sets is
1 5
Union sets is
9 7 16 17 5 1