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 <string.h>
typedef struct node
{
int data;
struct node *next;
}Node;
/* function declaration */
Node *insert_node(Node *head_ref,int n);
void display(Node *head_ref);
Node *get_union(Node *head_ptrA,Node *head_ptrB);
Node *get_intersection(Node *head_ptrA,Node *head_ptrB);
int compare(Node *head,int data);
/* Variable Declaration */
static Node *head1,*head2,*tail,*newnode,*head_union,*head_intersection,*res_union,*res_intersection;
int main()
{
int node_A_elements=0,node_B_elements=0,list_A_items=0,list_B_items=0;
/* List A Elements */
printf("Enter no. of elements in node A = n");
fflush(stdout);
scanf("%d",&node_A_elements);
printf("nEnter Elements in node A = ");
for(int i=0;i<node_A_elements;i++)
{
fflush(stdout);
scanf("%d",&list_A_items);
head1=insert_node(head1,list_A_items);
}
display(head1);
/*List B Elements */
printf("nnEnter no. of elements in node B = ");
fflush(stdout);
scanf("%d",&node_B_elements);
printf("nEnter Elements in node B = ");
for(int i=0;i<node_B_elements;i++)
{
fflush(stdout);
scanf("%d",&list_B_items);
head2=insert_node(head2,list_B_items);
}
display(head2);
/*Union of List A & B */
res_union=get_union(head1,head2);
printf("nResult of uniont");
display(res_union);
/*Intersection of List A & B */
res_intersection=get_intersection(head1,head2);
printf("nResult of Intersectiont");
display(res_intersection);
}
/* Creating link list using node concept */
Node *insert_node(Node *head_ref,int n)
{
newnode=(Node*) malloc(sizeof(Node));
printf("nAddress of node is %d",*newnode);
newnode->data=n;
printf("nThe value is %dn",newnode->data);
newnode->next=head_ref;
head_ref=newnode;
return head_ref;
}
/* Forming union of data present in link A & B */
Node *get_union(Node *head_ptrA,Node *head_ptrB)
{
printf("nnThe Union of list A & Bn");
while(head_ptrA!=0)
{
head_union=insert_node(head_union,head_ptrA->data);
head_ptrA=head_ptrA->next;
}
while(head_ptrB!=0)
{
if(!compare(head_union,head_ptrB->data))
head_union=insert_node(head_union,head_ptrB->data);
head_ptrB=head_ptrB->next;
}
return head_union;
}
/* Forming intersection of data present in link A & B */
Node *get_intersection(Node *head_ptrA,Node *head_ptrB)
{
printf("nnThe Intersection of list A & Bn");
while(head_ptrA!=0)
{
if(compare(head_ptrB,head_ptrA->data))
head_intersection=insert_node(head_intersection,head_ptrA->data);
head_ptrA=head_ptrA->next;
}
return head_intersection;
}
/* to perform compare operation */
int compare(Node *head,int data)
{
while(head!=NULL)
{
if(head->data==data)
return 1;
head=head->next;
}
return 0;
}
/* To perform display function */
void display(Node *head_ref)
{
printf("nThe values are");
if(head_ref==0)
{
printf("n empty list");
}
else
{
tail=head_ref;
while(tail!=0)
{
printf("t%d",tail->data);
tail=tail->next;
}
tail=newnode;
}
}