Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Overview of the Project:
A set is an unordered collection of data elements which can be described as elements or members of that particular set. Few popular operations can be performed on these sets which are intersection(list of all common elemets/members of two or more sets) union(list of all elements/members in two or more than two sets without repetition). Here in this project we use the concept of linked list(singly linked list) to store elemets from the user inputs to form a set. Two such set element data is taken through the user input and union, intersection operation is performed on them using the help and concepts of 'C' programming. The calculation is done and the union and intersection of the two sets is printed on the user console.
Goals:
- Taking user based choice of elements as inputs for both the sets.
- Displaying the inputs entered by the user on the console.
- Formulating and incorporating the union and intersection algorithms.
- Printing the output i.e., the union and the intersection of the two sets.
Key Highlights of the Project:
- Creating two sets of data using linked list concept of c programming that to the elements of user choice.
- Inserting node by node elements/members for both the sets.
- Implementing two seperate functions for Union and Intersection operation.
- Printing the union and intersection of both the lists after calculating using functions.
Deliverables of this Project:
- A user friendly interface for the user to enter elements/members of sets.
- Computation of Intersection of the provided two sets by the user.
- Computation of Union of the provided two sets by the user.
- Printing both the Union and Intersection of the given sets.
Expected Outcomes:
- Student will get an opportunity to showcase, practice the skills and concepts of Linked List, Pointers, structures, functions of c programming.
- Comparision of elements of two different linked list to compute common and all the elements without repetition.
- Computation of Union and Intersection of the data elements/members of the given input sets.
- Modular code writing and implemetation.
Code:
#include<stdbool.h> //preprocessor directive
#include<stdio.h> //preprocessor directive for calling an a standard library functions
#include<stdlib.h> //preprocessor directive for an standard boolean expression
//Link list node
struct Node { int data; //the data called in the form an int data type
struct Node* next; //pointer next node value is stored in an struct data type
};
//Function declaration
void insert(struct Node** head_ref, int new_data);
int iffound(struct Node* head, int data);
//Function to get union of two linked lists head1 and head2
struct Node* funion(struct Node* head1, struct Node* head2)
{
struct Node* result=NULL;
struct Node *t1=head1, *t2=head2;
//Insert all elements of list1 to the result list
while(t1!=NULL)
{
insert(&result, t1->data);
t1=t1->next;
}
//Insert those elements of lists which are not present in result list
while(t2!=NULL)
{
if(!iffound(result, t2->data))
{
insert(&result, t2->data);
}
t2=t2->next;
}
return result;
}
//Function to get intersection of two linked lists head1 and head2
struct Node* fintersection(struct Node* head1, struct Node* head2)
{
struct Node* result=NULL;
struct Node* t1=head1;
//Traverse list1 and search each element of it in list2.if the element
while(t1!=NULL)
{
if(iffound(head2, t1->data))
{
insert(&result, t1->data);
}
t1=t1->next;
}
return result;
}
//A utility function to insert a node at the begining of a linked list
void insert(struct Node** head_ref, int new_data)
{
//allocate node
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;
}
//function to print a linked list
void printit(struct Node* node)
{
while(node!=NULL)
{
printf("\n [%d]\n",node->data); //print the node as data
node=node->next;
}
}
//function that returns true if data is present in linked list else return
int iffound(struct Node* head, int data)
{
struct Node* t=head;
while(t!=NULL)
{
if(t->data==data)
{
return 1;
}
t=t->next;
}
return 0;
}
void module()
{
printf(" **************************************************");
}
//Driver program to test above function
int main()
{
int i,n,m,temp;
module();
printf("\n Enter the number of Elements/Members for Set 1: "); //console input output statement
fflush(stdout); //to find the bug or to clear the error
scanf("%d", &n); //console input output statement
printf("\n Enter the number of Elements/Members for Set 2: "); //console input output statement
fflush(stdout); //to find the bug or to clear the error
scanf("%d", &m); //console input output statement
printf("\n");
module();
//Start the structure pointer with the empty list
struct Node* head1=NULL; //structure node for head1
struct Node* head2=NULL; //structure node for head2
struct Node* intersecn=NULL; //structure node for intersection
struct Node* unin=NULL; //structure node for union
//for loop for link list1
for(i=0;i<n;i++) //here loop got executed
{
printf("\n Enter the value of Element/Member[%d] for Set 1:- ",i+1); //console input output statement
fflush(stdout); //to find the bug or to clear the error
scanf("%d", &temp); //console input output statement
insert(&head1, temp);
}
printf("\n");
module();
//for loop for link list2
i=0;
printf("\n");
for(i=0;i<m;i++)
{
printf("\n Enter the value of Element/Member[%d] for Set 2:- ",i+1); //console input output statement
fflush(stdout); //to find the bug or to clear the error
scanf("%d", &temp);
insert(&head2,temp);
}
printf("\n");
module();
//call the function of intersection
intersecn=fintersection(head1, head2);
//call the function of union
unin=funion(head1, head2);
printf("\n Elements/Members of Set 1:\n");
//function calling with structure pointer as argument
printit(head1);
printf("\n");
module();
printf("\n Elements/Members of Set 2:\n");
printit(head2);
printf("\n");
module();
printf("\n Intersection of Set 1 & Set 2 is:\n");
printit(intersecn);
printf("\n");
module();
printf("\n Union of Set 1 & Set 2 is:\n");
printit(unin);
printf("\n");
module();
return 0;
}
Code Screenshot:





Output Screenshot:


