Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
}*List1,*List2,*Union,*Intersection;
void initialize()
{
List1 = List2 = NULL;
}
void insert( struct node**head, int n)
{
struct node* newnode = (struct node*) malloc(sizeof(struct node));
newnode->data = n;
newnode->next = *head;
*head = newnode;
}
int search(struct node *head , int n)
{
while (head != NULL)
{
if(head->data ==n)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* locateunion(struct node *List1, struct node *List2)
{
Union = NULL;
struct node *temp = List1;
while(temp != NULL)
{
insert(&Union, temp->data);
temp = temp -> next;
}
while(List2 != NULL)
{
if(!search(List1,List2->data))
{
insert(&Union,List2->data);
}
List2 = List2 ->next;
}
return Union;
}
struct node* intersection(struct node *List1,struct node *List2)
{
Intersection = NULL;
while(List1 != NULL)
{
if(search(List2,List1->data))
{
insert(&Intersection,List1->data);
}
List1=List1->next;
}
return Intersection;
}
void printlinkedlist( struct node *ptr)
{
while (ptr != NULL)
{
printf("%d",ptr->data);
ptr = ptr->next;
if(ptr!=NULL)
printf("-->");
}
}
int main()
{
int i, count1, count2,temp;
initialize();
printf("enter nodes in first linked list");
scanf("%d",&count1);
printf("enter %d integersn", count1);
for(i=0; i<count1;i++)
{
scanf("%d", &temp);
insert(&List1,temp);
}
printlinkedlist(List1);
printf(" enter nodes in second linked list");
scanf("%d",&count2);
printf(" enter %d integersn", count2);
for(i=0;i<count2;i++)
{
scanf("%d",&temp);
insert(&List2,temp);
}
printlinkedlist(List2);
locateunion(List1,List2);
intersection(List1,List2);
printf(" Union Linked list");
printlinkedlist(Union);
printf("nIntersection Linked List");
printlinkedlist(Intersection);
return 0;
}
// i tired this program by my own but i have not got this so i have taken reference of skill link premium
and i clearly understand the concept thank you.