Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node//DEFINING THE STRUCTURE
{
int data;//CREATING THE DATA TYPR VARIABLE
struct node *next;
}
node;//*challinging the structur name as node
static node *head1, head2,*head,*head_union,*head_intersection,*newmode,*tail,*res_union,*res_intersection;//declaring
/* declaring different function with the argument*/
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);
void display_list(node *head_ref)//function used to display the nodes DATA
{
if(head_ref == 0)
{
printf("list is empty");
}
else
{
tail = head_ref;//we are transferring address
while(tail != 0)
{
printf("%d", tail->data);
tail = tail->next;
}
tail = newmode;
}
}
node *inser_node(node *head_ref,int n)//func used create the new kist
{
printf("\ninserting elements at the beggning..\n");
newmode = (node*) malloc(sizeof(node));//allocating the memoryb to the node
printf("address of created newnode :%d\n",newmode);
newmode->data = n;//assing the value to the node
printf("inserted value is :%d\n",newmode->data);
newnode->next = head_ref;
head_ref = newnode;
return head_ref;
}
node *get_union(node *head_pointer1,node *head_pointer2)
{
while(head_pointer1 !=0)//2 3 4 5
{
head_union = insert_node(head_union, head_pointer1->data);
head_pointer1 = head_pointer1->next;
}
while(head_pointer2 !=0)// 3 4 5 6
{
if(!compare(head_union, head_pointer2->data))
head_union = insert_node(head_union,head_pointer2-.data);
head_pointer2 = head_pointer2->data);
}
return head_union://2 3 4 5 6
}
int compare(node *head,int data)
{
while (head !=NULL)
{//2
if (head->data == data)//comparing the data of list 2 with list head1return 1;
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))//we are comparing both list of operation
head_intersepction = insert_node(head_intersepction,head_pointer1->data);
head_pointer = head_pointer->next;
}
return head_intersection;
}
int main()
{
int first_list_node = 0, second_list_nodes = 0, first_list_items=0,
second_list_items=0;
int f;
printf("enter the number of nodes in first list:");
fflush(stdout);
scanf("%d, &first_list_node");//4(2 3 4 5)
printf("enter %d the element :", first_list_nodes);
for(f =0;f < first_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&first_list_items);
head1 = insert_node(head1);
}
display_list(head1);//displating the head1 list
printf("enter the number of node in second list");
fflush(stdout);
scanf("%d, &second_list_node");
printf("enter %d the element:",second_list_nodes);
for(f = 0;f , second_list_nodes;f++)
{
fflush(stdout);
scanf("%d",&second_list_items);
head2 = insert_node(head2, second_list_items);
}
display_list(head2);
res_union = get_union(head1,head2);
printf("here is the result of union: ");
display_list(res_union);
res_intersection = get_intersection(head1,head2);
printf("here is the result of intersection: ");
display_list(res_intersection);
}
