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
OVERVIEW OF THE PROJECT:
In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two lists of data and this program will give the output as the union and intersection of these two lists
AIM OF THE PROJECT:
In this project, we are asked to find the set operations union and the intersection of two given linked lists.
/*
* file name:unioninter.c
* objective:Creation of user defined data type to implement
* the user interfaces for working with ‘Set’ (Mathematical Set theory)
* Created on: 11-oct-2022
* Author: Zeba khanum
*/
//preprocessor section
#include
#include
//self-referencing structure
struct Node
{
int data;
struct node *next;
};
//function insert to allocate memory to the elements dynamically ans assign the values
void insert(struct node**address, int data)
{
struct Node* new_node =(struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = (*address);
(*address) = new_node;
}
//function to perform search operation to compare the list 1 and the list 2 by traversing
int search(struct Node* head, int data)
{
struct Node* temp = head;
while (temp != NULL)
{
if (temp->data == data)
return 1;
temp = temp->next;
}
return 0;
}
//function to perform union operation
struct Node* union_func(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
struct Node *temp1 = list1, *temp2 = list2;
while (temp1 != NULL)
{
insert(&result, temp1->data);
temp1 = temp1->next;
}
while (temp2 != NULL)
{
if (!search(result, temp2->data))
insert(&result, temp2->data);
temp2 = temp2->next;
}
return result;
}
//function to perform intersection operation
struct Node* intersection(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
struct Node* temp1 = list1;
while (temp1 != NULL)
{
if (search(list2, temp1->data))
insert(&result, temp1->data);
temp1 = temp1->next;
}
return result;
}
//function to print the linked list
void printList(struct Node* ptr)
{
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
//main function
int main()
{
//local variable declaration and initialization
int i, n, m, temp;
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* intersect_list = NULL;
struct Node* union_list = NULL;
//printf() function to print on console
printf("Enter number of nodes in first Linked List\n");
fflush(stdout);
scanf("%d", &n);
printf("Enter %d integers\n", n);
fflush(stdout);
for(i=0; i
OUTPUT:
