Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
Project.c=
#include"list.h"//header//
int main()//main function//
{
int first_list_nodes=0,second_list_nodes=0,first_list_items=0,//local variable// //
second_list_items=0;//variable declaration//
printf("\nEnter the number of nodes in first list:");//displayed the massage to the user to enter the first list//
fflush(stdout);//cleared cache memory//
scanf("%d",&first_list_nodes);//asked user how many list numbers he is going to enter//
printf("\nEnter %d the elements:",first_list_nodes);
for(int i=0;i < first_list_nodes;i++)//for loop for the scanning the diffrent values//
{
fflush(stdout);//cleared cache memory//
scanf("%d",&first_list_items);//read the list items//
head1=insert_node(head1,first_list_items);
}
display_list(head1);//display list head1//
printf("\nEnter the number of nodes in second list:");//asked user to enter the second list//
fflush(stdout);//cleared Cache memory//
scanf("%d",&second_list_nodes);//Asked user to enter the second list//
printf("\nEnter %d the elements:",second_list_nodes);
for(int i=0;i < second_list_nodes;i++)//for loop to scan the Second list//
{
fflush(stdout);//cleared the cache memory//
scanf("%d",&second_list_items);//read the list items of the second list//
head2=insert_node(head2,second_list_items);
}
display_list(head2);//diplay list head2//
res_union = get_union(head1,head2);
#ifdef DEBUG_MAIN//this is we need to declare in the header file whenever we need to excute the program//
printf("\nValue of Head1 and Head2 :%d %d\n",head1,head2);
#endif
#ifdef DEBUG_MAIN
printf("\nvalue of Head1 and Head2 after performing union :%d %d\n",head1,head2);
printf("\nValue of res_union :%d\n",res_union);
#endif
printf("Here is the result of union:");
display_list(res_union);
res_intersection=get_intersection(head1,head2);//reinter section of the head1 and 2//
#ifdef DEBUG_MAIN
printf("\nValue of Head1 and head2 after performing union :%d %d\n",head1,head2);
printf("\nValue of res_intersection :%d\n",res_intersection);
#endif
printf("\nHere is the result of intersection:");//display the massage to the user result of intersection before diplaying the output//
display_list(res_intersection);//call the intersection to compare the two lists.//
}
List.c=
#include"list.h"
void display_list(Node *head_ref)
{
if(head_ref==0)//if statement if user dose not enters any number the it will show the massagelist is empty//
{
printf(" list is empty");
}
else
{
tail=head_ref;
while(tail !=0)
{
printf("%d", tail->data);
tail=tail->next;
}
tail=newnode;
}
}
Node *insert_node(Node *head_ref,int n)
{
printf("\nInserting Element at the beginning...\n");//insert the massage to the user//
newnode=(Node*)malloc(sizeof(Node));
printf("Address of created newnode :%d\n",newnode);//display the address of the first list items address//
newnode->data=n;
printf("Inserted value is :%d\n",newnode->data);//display the values of the list 1 items data//
newnode->next =head_ref;
head_ref=newnode;
return head_ref;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)
{
while(head_pointer1 !=0)//check whether the head pointer is not equal to 0//
{
head_union= insert_node(head_union,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
while(head_pointer2 !=0)//check whether the head pointer2 is not equal to 0//
{
if(! compare(head_union,head_pointer2->data))
head_union=insert_node(head_union,head_pointer2->data);
head_pointer2=head_pointer2->next;
}
return head_union;
}
int compare(Node *head,int data)
{
while(head != NULL)
{
if(head->data == data)
return 1;
head = head->next;
}
return 0;
}
Node *get_intersection(Node *head_pointer1,Node*head_pointer2)//get the intersection output of the both//
{
while (head_pointer1 !=0)//check whether the head pointer is not equal to 0//
{
if(compare(head_pointer2,head_pointer1->data))
head_intersection = insert_node(head_intersection,head_pointer1->data);
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}
List.h
#ifndef LIST_H_
#define LIST_H_
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node *next;
}Node;//user define data typedef declaration data and node//
static Node *head1,*head2,*head_union,*head_intersection,*newnode,*tail,*res_intersection,*res_union;//static variable declarations//
Node *insert_node(Node *head_ref,int n);//head ref data//
void display_list(Node *head_ref);
Node *get_union(Node *head_pointer1,Node *head_pointer2);
Node *get_intersection(Node *head_pointer1,Node *head_pointer2);
int compare(Node *head,int data);//compare the values of the both the list//
#endif /* LIST_H_ */