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<stdbool.h>
void createlist1(int);
void createlist2(int);
void display1();
void inter();
struct list
{
int value;
struct list *nxt;// Creating the two structure pointers
}*fnode1,*fnode2;
bool Present(struct list* headptr , int val);
struct list* caluni(struct list*,struct list*);
void push(struct list** ,int);
void printlist(struct list* prnt);
int main()
{
int n1,n2;
struct list* unin = NULL;
printf("\nEnter the number of values in List 1:");
scanf("%d",&n1);
createlist1(n1);
printf("\nThe values in the list 1:");
printlist(fnode1);
printf("\nEnter the number of values in List 2:");
scanf("%d",&n2);
createlist2(n2);
printf("\nThe values in the list 2:");
printlist(fnode2);
printf("\nThe intersection of two lists are:");
inter();
printf("\n The union of the two list are:");
unin = caluni(fnode1,fnode2);
printlist(unin);
return 0;
}
void createlist1(int n) // Creating first list
{
struct list *on1,*nn1;
int num1,i;
fnode1 = (struct list*)malloc(sizeof(struct list));
if (fnode1 == NULL)
{
printf("Memory can't be allocated");
}
else
{
printf("Enter the values for node 1");
scanf("%d",&num1);
fnode1->value = num1;
fnode1->nxt = NULL;
on1 = fnode1;
for (i=2;i<=n;i++)
{
nn1=(struct list*)malloc(sizeof(struct list));
if(nn1 == NULL)
{
printf("Memory can't be allocated");
}
else
{
printf("Enter the number of value for node %d",i);
scanf("%d",&num1);
nn1->value=num1;
nn1->nxt = NULL;
on1->nxt=nn1;
on1=on1->nxt;
}
}
}
}
void createlist2(int n) // Creating second list
{
struct list *on2,*nn2;
int num2,i;
fnode2 = (struct list*)malloc(sizeof(struct list));
if (fnode2 == NULL)
{
printf("Memory can't be allocated");
}
else
{
printf("Enter the values for node 1");
scanf("%d",&num2);
fnode2->value = num2;
fnode2->nxt = NULL;
on2 = fnode2;
for (i=2;i<=n;i++)
{
nn2=(struct list*)malloc(sizeof(struct list));
if(nn2 == NULL)
{
printf("Memory can't be allocated");
}
else
{
printf("Enter the number of value for node %d",i);
scanf("%d",&num2);
nn2->value=num2;
nn2->nxt = NULL;
on2->nxt=nn2;
on2=on2->nxt;
}
}
}
}
void printlist(struct list* prnt) // Printing second list
{
if(prnt==NULL)
{
printf("List is empty!");
}
else
{
while(prnt != NULL)
{
printf("%d ",prnt->value);
prnt=prnt->nxt;
}
}
}
void inter()
{
struct list *l1,*l2;
l1 = fnode1;
l2 = fnode2;
for (l2=fnode2;l2!=NULL;l2=l2->nxt)
{
for (l1=fnode1;l1!=NULL;l1=l1->nxt)
{
if(l1->value == l2->value)
{
printf("%d ",l1->value);
}
}
}
}
struct list* caluni(struct list* headl1,struct list* headl2) // Function for union
{
struct list* unilist = NULL; // Initiating new null pointer
struct list *ptr1 = headl1, *ptr2 = headl2;
while(ptr1 != NULL)
{
push(&unilist,ptr1->value);
ptr1= ptr1->nxt;
}
while(ptr2 != NULL)
{
if(!Present(unilist,ptr2->value))
push(&unilist,ptr2->value);
ptr2= ptr2->nxt;
}
return unilist;
}
void push(struct list** faddr,int new_data)
{
struct list* new_node = (struct list*)malloc(sizeof(struct list));
new_node->value = new_data;
new_node->nxt = *faddr;
*faddr = new_node;
}
bool Present(struct list* headptr, int val)
{
struct list* t = headptr;
while (t!=NULL)
{
if(t->value == val)
return 1;
t= t->nxt;
}
return 0;
}