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
- This project is about performing various mathematical set operations using linked list.
- There are two list in which user have to add elements to perform operations and get the outputs.
- The operations are based on mathematical set theory i.e. union and intersection of the sets.
- The elements of the set are compared and on the basis of comparision the union and intersection output is to be determined.
Goals:
- Providing users the output of their choice of set using C programming.
- The union and intersection to be performed easily.
Key Highlights of the project:
- Creating lists using linked list method.
- Displaying the created lists,
- Getting the result of union of data which are created by lists.
- Getting the result of intersection of data which are created by lists.
Deliverables of this project:
- Comparision of data of two lists.
- Union of data of two lists.
- Intersection of data of two lists.
Expected outcomes:
- User friendly interface.
- Gain knowledge about mathematical set operations.
- Comparison of two lists.
- Correct Union and Intersection data of two list.
Program code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node //structure defining
{
int data;
struct node *next; //self referential structure
}
Node; //aliasing name as Node
static Node *head, *head1, *head2, *head_union, *head_intersection, *newnode, *tail, *res_union, *res_intersection;
//Declaring different sub-function with the 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);
//creating function to display the nodes data
void display_list(Node *head_ref)
{
if(head_ref == 0)
{
printf("There are no results.");
}
else
{
tail = head_ref; //else transfer of address to tail
while(tail != 0)
{
printf("%d", tail->data); //prints the data of current node
tail = tail->next; //moving to the next node
}
tail = newnode;
}
}
//Creating function to create new list
Node *insert_node(Node *head_ref, int n)
{
printf("Inserting elements at the beginning...\n");
newnode =(Node*) malloc(sizeof(Node)); //allocation of memory to the nodes
printf("Address of created newnode :%d\n", newnode);
newnode-> data = n; //assigning the values to nodes
printf("Inserted value is %d\n", newnode->data);
newnode-> next = head_ref; //address of the previous address is transfered to the newnode
head_ref = newnode; //transfering the address of the new data to the head ref for linking newnode
return head_ref;
}
//Creating funciton to union the list
Node *get_union(Node *head_pointer1, Node *head_pointer2)
{
while(head_pointer1 != 0)
{
head_union = insert_node(head_union,head_pointer1->data); //transferring the data of list 1 into the union funciton
head_pointer1 = head_pointer1->next; //transverse the node using the address in next.
}
while(head_pointer2 != 0)
{
if(!compare(head_union,head_pointer2->data)) //transferring the element from list2 to union which are not already present in union
head_union = insert_node(head_union,head_pointer2->data);
head_pointer2 = head_pointer2->next; //travelling to next node
}
return head_union;
}
//Creating function to compare lists
int compare(Node *head, int data)
{
while(head != NULL)
{
if (head->data == data) //comparing the data of list 2 with list 1
return 1;
head = head ->next;
}
return 0;
}
//Creating function to intersect the list
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)
{
while (head_pointer1 != 0)
{
if (compare(head_pointer2, head_pointer1->data))// we are comparing the both lists for intersection
head_intersection = insert_node(head_intersection, head_pointer1->data); //transferring the taht value to the intersection
head_pointer1 = head_pointer1->next;
}
return head_intersection;
}
//Main function
int main()
{
int first_list_nodes = 0, second_list_nodes =0, first_list_items = 0, second_list_items = 0;
int k;
printf("Enter the number of Nodes for the first list:");
fflush(stdout);
scanf("%d",&first_list_nodes);
printf("Enter the %d elments:", first_list_nodes);
for ( k =0; k < first_list_nodes; k++)
{
fflush(stdout);
scanf("%d", &first_list_items); //get user input data
head1 = insert_node(head1,first_list_items);
}
display_list(head1);
printf("\nEnter the number of Nodes for the second list:");
fflush(stdout);
scanf("%d", &second_list_nodes);
printf("Enter the %d elements:", second_list_nodes);
for(k = 0;k <second_list_nodes;k++)
{
fflush(stdout);
scanf("%d", &second_list_items); //get user input data
head2 = insert_node(head2,second_list_items);
}
display_list(head2);
res_union = get_union(head1,head2); //transferring the result of union funciton to the res union
printf("Here is the result of union: ");
display_list(res_union); //transferring the vaules as arguments to display funciton
res_intersection = get_intersection(head1,head2); //transferring the result of intersection funciton
printf("\nHere is the result of intersection: "); //transferring the result of intersection function
display_list(res_intersection); //transferring the values as arguments to display function
}