Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
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
Project1:
Aim:
To create interfaces for mathematical Set Theory implemented using C programming language.
Objective:
- To get the union and intersection operation performed for the two sets of datas given by the user.
- Implementation of interface for mathematical Set Theory using C programming.
Principle:
- Intersection of two sets:
- It is the set that contains all the elements that are common to both the sets.
- Example X={1,2,3} and Y={2,3,4} so the intersection gives {2,3}
- Union of two sets:
- It is the set containing all the elements of the given sets.
- Example X={1,2,3} and Y={2,3,4} so the union gives {1,2,3,4}
CODE :
#ifndef LIST_H_// macro is defined by list h//
#define LIST_H_// define list h//
#include //input output header file//
#include //standerd library header file//
#include //boolan data type header file
typedef struct node
{
int data;
struct node *next;
}Node;
static Node *head1,*head2,*head,*head_union,*head_intersection,*newnode,*tail,*res_union,*res_intersection;
Node *insert_node(Node *head_ref,int n);
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);
#endif
int main()
{
int first_list_nodes = 0,second_list_nodes = 0,first_list_items = 0,
second_list_items = 0;
printf("Enter the number of nodes in first list:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter %d the elements:",first_list_nodes);
for(int i = 0;i < first>
{
fflush(stdout);
scanf("%d",&first_list_items);
head1 = insert_node(head1,first_list_items);
}
display_list(head1);
printf("\n Enter the number of nodes in second list:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter %d the elements:",second_list_nodes);
for(int i = 0; i < second>
{
fflush(stdout);
scanf("%d",&second_list_items);
head2 = insert_node(head2,second_list_items);
}
display_list(head2);
res_union = get_union(head1,head2);
#ifdef DEBUG_MAIN
printf("\n Value of head1 and head2 :%d%d",head1,head2);)
#endif
#ifdef DEBUG_MAIN
printf("\n Value of head1 and head2 after performing union :%d%d\n",head1,head2);
printf("Value 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);
#ifdef DEBUG_MAIN
printf("\n Value of head1 and head2 after performing union:%d%d\n",head1,head2);
printf("Value of res_intersection:%d\n",res_intersection);
#endif
printf("Here is the result of intersection:");
display_list(res_intersection);
}
void display_list(Node *head_ref)
{
if(head_ref==0)
{
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 elements at the beginning...\n");
newnode =(Node*) malloc(sizeof(Node));
printf("Address of created newnode :%d\n",newnode);
newnode->data = n;
printf("Inserted value is:%d\n",newnode->data);
newnode-> next = head_ref;
head_ref = newnode;
return head_ref;
}
Node *get_union(Node *head_pointer1, Node *head_pointer2)
{
while(head_pointer1 !=0)
{
head_union=insert_node(head_union,head_pointer1->data);
head_pointer1=head_pointer1->next;
}
while(head_pointer2 !=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)
{
while(head_pointer1 !=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;
}
Output :
Enter the number of nodes in first list:4
Enter 4 the elements:2 4 6 8
Inserting elements at the beginning...
Address of created newnode :20507328
Inserted value is:2
Inserting elements at the beginning...
Address of created newnode :20507360
Inserted value is:4
Inserting elements at the beginning...
Address of created newnode :20507392
Inserted value is:6
Inserting elements at the beginning...
Address of created newnode :20507424
Inserted value is:8
8642
Enter the number of nodes in second list:4
Enter 4 the elements:3 6 9 4
Inserting elements at the beginning...
Address of created newnode :20507456
Inserted value is:3
Inserting elements at the beginning...
Address of created newnode :20507488
Inserted value is:6
Inserting elements at the beginning...
Address of created newnode :20507520
Inserted value is:9
Inserting elements at the beginning...
Address of created newnode :20507552
Inserted value is:4
4963
Inserting elements at the beginning...
Address of created newnode :20507584
Inserted value is:8
Inserting elements at the beginning...
Address of created newnode :20507616
Inserted value is:6
Inserting elements at the beginning...
Address of created newnode :20507648
Inserted value is:4
Inserting elements at the beginning...
Address of created newnode :20507680
Inserted value is:2
Inserting elements at the beginning...
Address of created newnode :20507712
Inserted value is:9
Inserting elements at the beginning...
Address of created newnode :20507744
Inserted value is:3
Here is the result of union:392468
Inserting elements at the beginning...
Address of created newnode :20507776
Inserted value is:6
Inserting elements at the beginning...
Address of created newnode :20507808
Inserted value is:4
Here is the result of intersection:46






Output :



Result:
Thus a program is created to interface mathematical Set Theory implemented using C language.