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: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:
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.
Two linked lists created and then created functions insert Node,Get Union,Get intersection,compare and display to perform the union and intersection to the list .
This program prints the two list of values and shows the union & intersection to the lists.
Goals:
*Implementation Of C interface for mathematical set theory using C progamming.
*Creating two lists of data using the linked list method and getting the output Union and intersection to the lists entered
*Display the data of two linked lists and union and intersection of the lists.
Key Highlights of the Project:
*Creating two list of data using linked list method.
*Insert the node to 1st and 2nd list accordingly.
*Intersection of data of two lists.
*Getting Union of two list.
*Displaying the values in union and intersection.
Deliverables of this Project:
*Taking the input of two list of values from console and display the values
*Getting union of values from the two list as per Mathematical Set theory.
*Getting intersection of values from the two list as per Mathematical Set theory.
Expected Outcomes:
*Student will get the Knowledge of creating the nodes using Linked List.
*Getting the intersection and Union of data of two list.
*Comparing the elements of two list.
*Funtions creating for insert node & display units,union,intersection and compare.
Code:
#include // Preprocessor section
#include
//
typedef struct node//defining the structure
{
int data;
struct node *next;
}node;//Changing the structure name to node
static node *h1,*h2,*tail,*Union,*intersection,*result_union,*result_intersection,*newnode;// declaring the different varable in structure type
//Declaring functions with arguments
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);
//Function Definitions
//Insert node function definition
node *insert_node(node*headnode,int n)
{
printf("Inserting Elements at the beginning...%dn",n);
newnode =(node*) malloc(sizeof (node));
newnode->data=n;
newnode->next=headnode;
headnode=newnode;
return headnode;
}
//Union function definition
node *get_union(node *head1,node *head2)
{
while(head1!=0)
{
Union=insert_node(Union,head1->data);
head1=head1->next;
}
while(head2!=0)
{
if( !compare(Union,head2->data))
Union=insert_node(Union,head2->data);
head2=head2->next;
}
return Union;
}
//Display unit function
void display_list(struct node *head)
{
if(head==0)
{
printf("List is empty");
}
else
{
tail=head;
while(tail!=0)
{
printf("%d ",tail->data);
tail=tail->next;
}
tail=newnode;
}
printf("n");
}
//Compare list function
int compare(node *headnode,int data)
{
while (headnode != NULL)
{
if (headnode->data ==data)//comparing the data of list 2 with list 1
{
return 1;
}
headnode = headnode->next;
}
return 0;
}
//Intersection Function
node *get_intersection(node *head1,node *head2)
{
while (head1!= 0)
{
if (compare(head2, head1->data))//we are comparing the both list for intersection
{
intersection = insert_node(intersection,head1->data);//transferring the value to the intersection
}
head1 = head1->next;
}
return intersection;
}
//Main Function
int main()
{
int first_list_nodes, second_list_nodes, first_list_items, second_list_items;
int f;
printf("Enter the number of Nodes in first List:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter %d the elements:", first_list_nodes);//getting the number of element in the first list
fflush(stdout);
for (f=0;f<first_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&first_list_items);
printf("Inserting %d itemn",first_list_items);
h1=insert_node(h1,first_list_items);
}
display_list(h1);
printf("Enter the number of Nodes in second List:");
fflush(stdout);
scanf("%d",&second_list_nodes);
printf("Enter %d the elements:", second_list_nodes);//getting the number of element in the second list
fflush(stdout);
for (f=0;f<second_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&second_list_items);
h2=insert_node(h2,second_list_items);
}
display_list(h2);
result_union=get_union(h1,h2);//Output union
printf("Result of Union:");
display_list(result_union);//displaying Output Union
result_intersection=get_intersection(h1,h2);//Output Intersection
printf("Result of intersection:");
display_list(result_intersection);//displaying Output Intersection
return 0;
}
Screenshot:

Output:
Enter the number of Nodes in first List:4
Enter 4 the elements:1
Inserting 1 item
Inserting Elements at the beginning...1
3
Inserting 3 item
Inserting Elements at the beginning...3
5
Inserting 5 item
Inserting Elements at the beginning...5
7
Inserting 7 item
Inserting Elements at the beginning...7
7 5 3 1
Enter the number of Nodes in second List:4
Enter 4 the elements:2
Inserting Elements at the beginning...2
4
Inserting Elements at the beginning...4
3
Inserting Elements at the beginning...3
9
Inserting Elements at the beginning...9
9 3 4 2
Inserting Elements at the beginning...7
Inserting Elements at the beginning...5
Inserting Elements at the beginning...3
Inserting Elements at the beginning...1
Inserting Elements at the beginning...9
Inserting Elements at the beginning...4
Inserting Elements at the beginning...2
Result of Union:2 4 9 1 3 5 7
Inserting Elements at the beginning...3
Result of intersection:3