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
// creat struct node for linked list
struct node{
int data;
struct node *next;
};
int i;
// create element of linked list
struct node* createlist()
{
int n;
struct node *head=NULL, *p, *ptr;
int x,i;
printf("enter no of elements in list :\n");
fflush(stdout);
scanf("%d",&n);
for(i=0;i
printf("enter the data value:");
fflush(stdout);
scanf("%d",&x);
p=(struct node*) malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if(head==NULL)
{
head=ptr=p;
}
else
{
ptr->next=p;
ptr=p;
}
}
return head;
}
// create list for display list1 and 2 element
void displaylist(struct node *list)
{
struct node *p;
p=list;
while(p!=0)
{
printf("%d\t",p->data);
p=p->next;
}
}
// create call function intersecn set for list1 & list2
int intersecn(struct node *list1, struct node *list2)
{
printf("\nIntersection list:\t");
fflush(stdout);
struct node *temp3 = list1;
struct node *temp4 = list2;
while(temp3){
while(temp4){
if (temp3->data==temp4->data)
{
printf("%d\t",temp3->data);
}
temp4=temp4->next;
}
temp3=temp3->next;
temp4=list2;
}
return 0;
}
// create call function unin set for list1 & list2
int unin(struct node *list1, struct node *list2)
{
printf("\n Union list:\t");
fflush(stdout);
struct node *temp3 = list1;
struct node *temp4 = list2;
int flag=1;
displaylist(temp3);
// if list1 not equal list2 go to while loop
while(temp4)
{
if(temp3->data!=temp4->data)
{
printf("%d\t",temp4->data);
}
temp4=temp4->next;
}
// if list1 equal list2 go to while loop
while (temp4) {
while (temp3) {
if(temp3->data==temp4->data)
{
flag=0;
}
temp3=temp3->next;
}
if(flag)
{
printf("%d",temp3->data);
}
temp4=temp4->next;
temp3=list1;
}
return 0;
}
// write main function
int main()
{
struct node *list1,*list2;
list1=list2=NULL;
printf("list 1:\n");
list1=createlist();
printf("list 2:\n");
list2=createlist();
printf("\nlist 1 is: ");
displaylist(list1);
printf("\nlist 2 is: ");
displaylist(list2);
intersecn(list1,list2);
unin(list1,list2);
}