Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include
#include
struct link
{
int data;
struct link* next;
};
void insert(struct link** head,int value)
{
struct link *local=(struct link*)malloc(sizeof(struct link));
local->data=value;
local->next=*head;
*head=local;
}
void disp(struct link* head)
{
while(head!=NULL)
{
printf("%d\n",head->data);
head=head->next;
}
}
int compare(struct link *list, int data)
{
while(list!=NULL)
{
if(list->data == data)
{
return 1;
}
list = list->next;
}
return 0;
}
struct link *get_union(struct link *L1, struct link *L2)
{
struct link *list_union = (struct link*)malloc(sizeof(struct link));
list_union=NULL;
struct link *temp = L1;
while(temp!=NULL)
{
insert(&list_union, temp->data);
temp=temp->next;
}
while(L2!=NULL)
{
if(!compare(L1, L2->data))
{
insert(&list_union, L2->data);
}
L2=L2->next;
}
return list_union;
}
struct link *get_intersection(struct link *L1, struct link *L2)
{
struct link * list_intersection = (struct link*)malloc(sizeof(struct link));
list_intersection=NULL;
while(L1!=NULL)
{
if(compare(L2,L1->data))
{
insert(&list_intersection,L1->data);
}
L1=L1->next;
}
return list_intersection;
}
int main()
{
int n1,n2,d1,d2;
struct link* list1=NULL;
struct link* list2=NULL;
struct link* union1 = NULL;
struct link* intersection = NULL;
printf("Enter the no.of elements in list1:");
scanf("%d", &n1);
printf("\nEnter the list1 elements:");
for(int i=0;i { scanf("%d",&d1); insert(&list1,d1); } printf("\nThe entered elements:"); disp(list1); printf("\nEnter the no.of elements in list2:"); scanf("%d", &n2); printf("\nEnter the list2 elements:"); for(int i=0;i { scanf("%d",&d2); insert(&list2,d2); } printf("\nThe entered elements:"); disp(list2); union1 = get_union(list1,list2); printf("\nThe union elements:"); disp(union1); intersection = get_intersection(list1,list2); printf("\nThe intersection elements:"); disp(intersection); return 0; }