Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
project1
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *list1=NULL; //creating lists and initialized with NULL
struct node *list2=NULL;
struct node *unionll=NULL;
struct node *intersectionll=NULL;
//inserting data into the list
void insert(struct node **head, int number) //**h store the address of the root pointer
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=number;
newnode->link=*head;
*head=newnode;
}
//printing data in the list
void print(struct node *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
fflush(stdout);
p=p->link;
}
printf("\n");
}
//this is used to search elements ,and compare the data in the list
int search(struct node *p,int num)
{
while(p!=NULL)
{
if(p->data==num)
{
return 1;
}
p=p->link;
}
}
//finding union of both list and returning type of struct node
struct node *unionof(struct node *list1,struct node *list2)
{
struct node *temp=list1;
while(temp!=NULL)
{
insert(&unionll,temp->data);
temp=temp->link;
}
while(list2!=NULL)
{
if(!search(list1,list2->data)) //if both elements are not same insert element into the unionll
{
insert(&unionll,list2->data);
}
list2=list2->link;
}
return unionll;
}
//finding intersection of both list and returning type of struct node
struct node *intersec(struct node *list1,struct node *list2)
{
while(list1!=NULL)
{
if(search(list2,list1->data))
{
insert(&intersectionll,list1->data);
}
list1=list1->link;
}
return intersectionll;
}
int main()
{
int i,list1_count,list2_count,num;
printf("enter no.of nodes in link1\n");
fflush(stdout);
scanf("%d",&list1_count);
printf("enter data in link1\n");
fflush(stdout);
for(i=0;i<list1_count;i++) //inserting list 1 elements
{
scanf("%d",&num);
insert(&list1,num);
}
print(list1); //printing list 1 elements
printf("enter no.of nodes in link2\n");
fflush(stdout);
scanf("%d",&list2_count);
printf("enter data in link2\n");
fflush(stdout);
for(i=0;i<list2_count;i++)
{
scanf("%d",&num); //inserting list 2 elements
insert(&list2,num);
}
print(list2); //printing list 2 elements
unionof(list1,list2); //calling union of function
printf("union of list1 and list2 is:\n");
fflush(stdout);
print(unionll);
intersec(list1,list2); //calling intersec function
printf("intersection of list1 and list2 is:\n");
fflush(stdout);
print(intersectionll);
return 0;
}
