Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Instructor:Abinaya Inbamani
// In this project, interfaces for mathematical Set Theory are implemented using C programming language. User can give the input like two list of data and this program will give the output as the union and intersection of these two lists
#include<stdio.h>//for standard input and output
#include<stdlib.h>//for performing memory allocation
#include<math.h>//to perform mathematical operation
typedef struct node // definition of structure
{
int data_list;
struct node *next;
}Node;
static Node *head_1, *head_2 , *head , *head_list_union , *head_list_intersection , *new_node , *ta_il;
static Node *res_union, *res_intersection;
Node *insert_node(Node *head_ref, int no);
void display_list(Node *head_ref);
Node *get_union(Node *head_pointer_1, Node *head_pointer_2);
Node *get_intersection(Node *head_pointer_1, Node *head_pointer_2);
int compare(Node *head, int data_list);
void display_list(Node *head_ref) //function used to display the nodes data
{
if(head_ref==0)//condition checking
{
printf("\n The list doesnt contains anything or is empty \n"); // if condition is met then nothing is in list
}
else // if the condition doesnt satisfies then this prevails i.e. head_ref is not zero
{
ta_il=head_ref; //address transferred to the tail
while(ta_il!=0) // repeating the loop till we reach the end of the list
{
printf("%d", ta_il->data_list); //prints the data of current node
ta_il=ta_il->next; //moving on to the new node
}
ta_il=new_node; //tail is given the value of new node
}
}
Node *insert_node(Node *head_ref,int no)
{
printf("\n Inserting elements at the beginning");
new_node=(Node*)malloc(sizeof(Node));//allocating the memory to nodes by malloc
printf("\n Address of the created node: %d \n",new_node);
new_node->data_list=no; //assigning the value to the node by using the arrow in structure
printf("Inserted value is: %d \n ", new_node->data_list);
new_node->next=head_ref;//accessing the address of the next location
head_ref=new_node;// transferring the new node to the head ref for linking new node
return head_ref;//returning the head refence value as int is used
}
Node *get_union(Node *head_pointer_1, Node *head_pointer_2)//fuction used to perform union on the list
{
while(head_pointer_1!=0)//repeating the value till the head_pointer becomes 0
{
head_list_union=insert_node(head_list_union,head_pointer_1->data_list);// here we are inserting the data of list head_pointer1-
head_pointer_1=head_pointer_1->next; // head pointer 1 updates its value from the next address.
}
while(head_pointer_2!=0) //repeating till the value of head_pointer_2 becomes 0
{
if(!compare(head_list_union,head_pointer_2->data_list))//condition to compare the two lists
head_list_union=insert_node(head_list_union,head_pointer_2->data_list);//inserting the elements missing in the list head_pointer2-
head_pointer_2=head_pointer_2->next; //reading the data in the next node
}
return head_list_union;//returning the value of head union
}
int compare(Node *head,int data_list) //comparing the two lists
{
while(head!=NULL)//repeating till the list becomes empty
{
if(head->data_list==data_list) //comparing the data of the list 2 with the data of list head_pointer_1
return 1;
head=head->next;
}
return 0;
}
Node *get_intersection(Node *head_pointer_1, Node *head_pointer_2)
{
while(head_pointer_1!=0) //repeat till the head pointer 1 becomes 0
{
if(compare(head_pointer_2,head_pointer_1->data_list))//comparing the two lists
{
head_list_intersection=insert_node(head_list_intersection,head_pointer_1->data_list);//storing the elements common between the two lists
head_pointer_1=head_pointer_1->next;//updating the head_pointer_1 value to the next address
}
return head_list_intersection;//returning the intersected list
}
}
int main()
{
int first_list_nodes=0,second_list_nodes=0, first_list_items=0,second_list_items=0; // initial declaration of the nodes and the values to be 0
int i;
printf("\n enter the number of nodes in the first list\n");//asking the user to give the number of nodes of the two lists
fflush(stdout); //clearing the cache
scanf("%d",&first_list_nodes); //reading that how many nodes first list has
printf("enter %d the elements:",first_list_nodes);//getting the number of elements in the first list
for(i=0;i<first_list_nodes;i++) //loop for repeatatively getting the items stored in the nodes
{
fflush(stdout);//clearing the cache
scanf("%d",&first_list_items);//getting the data in the runtime
head_1=insert_node(head_1,first_list_items);//saving the values in head_1
}
display_list(head_1); //displaying the head1 list
printf("\n enter the number of nodes in second list:");//similarly asking for the second node
fflush(stdout);//clearing the cache
scanf("%d",&second_list_nodes);//reading the number of second list nodes
printf("enter %d the elements:",second_list_nodes);//printing the number of elements to be entered
for(i=0;i<second_list_nodes;i++)//using the loop for second list items
{
fflush(stdout);//clearing the cache
scanf("%d",&second_list_items);//getting data in the runtime
head_2=insert_node(head_2,second_list_items);//entering the data in the head_2 i.e. insterting the data of the second list
}
display_list(head_2);//display the second list
res_union=get_union(head_1,head_2);//calling the getunion function just to union two lists
printf("\n the result of union is:");//printing the result of union of two lists
display_list(res_union); //calling the display list function to display the unioned lists
res_intersection=get_intersection(head_1,head_2); // similarly get intersection function is called and stored the value in variable res_intersection
printf("\n the result of intersection is:"); //displaying the result of intersection
display_list(res_intersection);//now displaying the res_intersection to display the intersection values
}