Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/********** Project 1
Create two lists with size of list given by user
1.fing out the unique elements in list
2.find out common elements in lists
3.print the union and intersection elements in lists**************/
/*** Name:Vemana Niranjan
email:[email protected] *****/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
/** above lines are structure declaration and header files needed ***/
// below are function declarations
struct node *head1=NULL,*head2=NULL,*unio_n=NULL,*intersection=NULL;
struct node * create_list1(struct node *head1,int a);
struct node * create_list2(struct node *head2,int b);
struct node *create_intersection_list(struct node *head1,struct node *head2);
struct node *create_union_list(struct node *head1,struct node *head2);
void display_list(struct node*t);
int main()
{
int a,b;
printf("enter no of elements in list 1:");
scanf("%d",&a);//scanning no of elements and creating list
head1=create_list1(head1,a);
printf("enter no of elements in list 2:");
scanf("%d",&b);
head2=create_list2(head2,b);//scanning no of elements and creating list
intersection=create_intersection_list(head1,head2);//fun call to create intersection
unio_n=create_union_list(head1,head2);//fun call to create union
printf("intersection list:");
display_list(intersection);
printf("\n");
printf("union list:");
display_list(unio_n);
return 0;
}
void display_list(struct node*t)//taking head addr of the list to display list elements
{
if(t==NULL)
{
printf("list is empty in this category\n");
return;
}
struct node *temp=t;
while(temp!=NULL)//travering and printing elements
{
printf("%d ",temp->data);
temp=temp->next;
}
}
struct node * create_list1(struct node *head1,int a)//need list 1 head addr to create list
{
struct node *ptr,*temp;
int num;
printf("enter elements in list 1 one by one\n");
for(a;a>0;a--)//loop to scanf multiple nodes data
{
scanf("%d",&num);
ptr=(struct node *)malloc(1*sizeof(struct node));//dynamic memory allocation
ptr->data=num;//data assingment
if(head1==NULL)
{
ptr->next=NULL;
head1=ptr;
}
else
{
temp=head1;
while(temp->next!=NULL)//traversing to last element
temp=temp->next;
temp->next=ptr;//adding new node address to current last node of list
ptr->next=NULL;
}
}
return head1;
}
struct node * create_list2(struct node *head2,int b)//need list 2 head addr to create list
{
struct node *ptr,*temp;
int num;
printf("enter elements in list 2 one by one\n");
for(b=b;b>0;b--)//loop to scanf multiple nodes data
{
scanf("%d",&num);
ptr=(struct node *)malloc(1*sizeof(struct node));//dynamic memory allocation
ptr->data=num;//data assingment
if(head2==NULL)
{
ptr->next=NULL;
head2=ptr;
}
else
{
temp=head2;
while(temp->next!=NULL)//traversing to last element
temp=temp->next;
temp->next=ptr;//adding new node address to current last node of list
ptr->next=NULL;
}
}
return head2;
}
struct node *create_intersection_list(struct node *head1,struct node *head2)
{
struct node *ptr,*temp,*temp1,*temp2;
temp=head1;
while(temp!=NULL)//two while loops to check 1 by 1 element to all elements in list 2
{
temp2=head2;
while(temp2!=NULL)
{
if(temp->data==temp2->data)
{
ptr=(struct node *)malloc(1*sizeof(struct node));
ptr->data=temp->data;
//above 2 ops same as creation list
if(intersection==NULL)//condition to insert as 1st node or last node of list
{
ptr->next=NULL;
intersection=ptr;
break;
}
else
{
temp1=intersection;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=ptr;
ptr->next=NULL;
}
break;
}
temp2=temp2->next;
}
temp=temp->next;//travering condition
}
return intersection;//returning list base addr
}
struct node *create_union_list(struct node *head1,struct node *head2)
{
struct node *ptr,*temp,*temp1,*temp2,*temp3;
temp=head1;
while(temp!=NULL)//two while loops to check 1 by 1 element to all elements in list 2
{
temp2=head2;
while(temp2!=NULL)
{
if(temp->data!=temp2->data)
temp2=temp2->next;
else
break;
}
if(temp2==NULL)//condition to check element is unique or not
{
ptr=(struct node *)malloc(1*sizeof(struct node));
ptr->data=temp->data;
if(unio_n==NULL)
{
ptr->next=NULL;
unio_n=ptr;
}
else
{
temp3=unio_n;
while(temp3->next!=NULL)
temp3=temp3->next;
temp3->next=ptr;
ptr->next=NULL;
}
}
temp=temp->next;
}
//we are doing twice because possibility of list 2 data is unique
temp=head2;
while(temp!=NULL)//two while loops to check 1 by 1 element to all elements in list 2
{
temp2=head1;
while(temp2!=NULL)
{
if(temp->data!=temp2->data)
temp2=temp2->next;
else
break;
}
if(temp2==NULL)//condition to check element is unique or not
{
ptr=(struct node *)malloc(1*sizeof(struct node));
ptr->data=temp->data;
//same operations as creation of list
if(unio_n==NULL)
{
ptr->next=NULL;
unio_n=ptr;
}
else
{
temp3=unio_n;
while(temp3->next!=NULL)
temp3=temp3->next;
temp3->next=ptr;
ptr->next=NULL;
}
}
temp=temp->next;
}
return unio_n;//returning list base addr
}