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
over view of project:
1.user can give the input like two list of data
2.user provide two set of datas A and B
3.first list A=[1,2,3,4] and second list B=[3,4,5,6]
4.then after we can apply the union and intersection of two set of first and second list
Goals:
- Implementation of interface for mathematical Set Theory using C programming.
- Giving two list of data as input to get the output as union & intersection for given list of data.
Key highlights of the project:
1. Creating two linked list of data using single linked list.
2. Insertion is using for insert the data from one data to another data accordingly.
3. Intersection of data of two list created
4. getting union of two list created
Deliverable of the Project:
1. Intersection is identify the same element of both First and Second list.
2. Here, interestion is indenty the 3,4 data is common in both the list.
3. Union it's help to arrange all the data from top to bottom order with help of two list. But it will not take 2 same data in both the list, it will take only data.
4. Here, union is arrange the both list : 6,5,4,3,2,1.
Expected outcomes:
1. Here using single linked list, it is the simplest form of a linked list.
2. Intersection data, traverse through the first list, and look for every element in the second list. If the element is present in the second list, then add that element to the result list.
3. Union data, traverse through the first list and add all of its elements to the result list. Now, traverse through the second list. If an element of the second list is already present in the result list.
#include<stdio.h>//preprocessor
#include<stdlib.h>
typedef struct Node//define the Node and user define data type
{
int data;
struct Node *Next;
}Node;
static Node//static variable declaration
*First_node,*Second_node,*First_newnode,*Second_newnode,*find_intersection,*find_union,*newnode,*head_intersection,*head_union,*temp1,*temp2;
Node *insert_node(Node *head_ref,int n)//this function is creating for inserting the element to linked list
{
newnode=(Node*)malloc(sizeof(Node));//memory allocation
newnode->data=n;
newnode->Next=head_ref;
head_ref=newnode;
return head_ref;
}
int compare(Node *head,int data)//this function is created for compare two linked list
{
while(head !=NULL)
{
if(head->data==data)
return 1;
head=head->Next;
}
return 0;
}
Node *get_union(Node *head_pointer1,Node *head_pointer2)//this function is created for to get union element
{
Node *temp=head_pointer1;//created for pointer temporary value is equal to pointer1
while(temp !=0)//check and whether the head pointer is not equal to 0 or NULL
{
head_union=insert_node(head_union,temp->data);
temp=temp->Next;
}
while(head_pointer2 !=0)
{
if(!compare(head_pointer1,head_pointer2->data))
head_union=insert_node(head_union,head_pointer2->data);
head_pointer2=head_pointer2->Next;
}
return head_union;
}
Node *get_intersection(Node *head_pointer1,Node *head_pointer2)//this function is created for to get intersection element
{
while(head_pointer1 !=0)//check and whether the head pointer is not equal to 0 or NULL
{
if(compare(head_pointer2,head_pointer1->data))
head_intersection=insert_node(head_intersection,head_pointer1->data);
head_pointer1=head_pointer1->Next;
}
return head_intersection;
}
//print a linked list from head node till tail node
void print_linkedlist(Node *nodeptr)
{
while (nodeptr !=NULL)
{
printf("%d",nodeptr->data);
nodeptr=nodeptr->Next;
if(nodeptr !=NULL)
printf("-->");
}
}
int main()//main function
{
int a,b,i;//local variable declaration
//memory allocation
temp1=malloc(sizeof (Node));
temp2=malloc(sizeof(Node));
//creating a first linked list
printf("Enter the number of element in first linked list\n");//user Enter the number of element in first linked list
fflush(stdout);
scanf("%d",&a);
//user enter the items of first linked list
printf("Enter the items of first linked list:\n");
fflush(stdout);
scanf("%d",&temp1->data);
temp1->Next=NULL;
First_node=temp1;
for(i=1;i<a;i++)
{
First_newnode=malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(First_newnode->data));
First_newnode->Next=NULL;
First_node->Next=First_newnode;
First_node=First_node->Next;
}
First_node=temp1;
//print the element of first linked list
printf("you Enter the first linked list:\n");
{
print_linkedlist(First_node);
}
//user Enter the number of element in second linked list
printf("\n Enter the number of element in second linked list\n");
fflush(stdout);
scanf("%d",&b);
//user enter the items of second linked list
printf("Enter the items of second linked list:\n");
fflush(stdout);
scanf("%d",&temp2->data);
temp2->Next=NULL;
Second_node=temp2;
for(i=1;i<b;i++)
{
Second_newnode=malloc(sizeof(Node));
fflush(stdout);
scanf("%d",&(Second_newnode->data));
Second_newnode->Next=NULL;
Second_node->Next=Second_newnode;
Second_node=Second_node->Next;
}
Second_node=temp2;
printf("you Entered the second linked list:\n");
{
print_linkedlist(Second_node);
}
//union of both linked list
find_union=NULL;
{
printf("\n union of First and Second linked list:\n");
}
find_union=get_union(First_node,Second_node);
print_linkedlist(head_union);
//intersection of both liked list
find_intersection=NULL;
{
printf("\nIntersection of First and second liked list:\n ");
}
//calling get intersection function
find_intersection=get_intersection(First_node,Second_node);
if(find_intersection==0)
{
printf("First and Second linked list not have common item");
}
print_linkedlist(head_intersection);
return 0;
}