Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include<stdio.h>
#include<stdlib.h>
//declaring function prototypes for insert,union,intersection,display,compare.
struct node *insert(struct node*head, int n);
void display(struct node *head);
struct node *fun_union(struct node*h1, struct node*h2);
struct node *fun_intersection(struct node*h1, struct node*h2);
int compare(struct node *headnode, int n);
//declaring a structure called node
struct node
{
int data;//these are structure members
struct node *next;
}*h1=NULL,*h2=NULL,*h3=NULL,*head,*temp,*temp1,*tail,*un=NULL,*intersection=NULL,*result_union,*result_intersection,*headnode=0,*newnode,*head1,*head2;//structure variables
int main()
{
int total_first,total_second,first_list_value,second_list_value;
printf("\n Linked List-1:Enter No.of Nodes to be Inserted:");
scanf("%d",&total_first);
int i;
//logic to give first list elements
for(i=0;i<total_first;i++)
{
printf("Enter Data %d:",i);
scanf("%d",&first_list_value);// storing value for no.of nodes we want
h1=insert(h1,first_list_value);//calling insert function passing arguments.
}
printf("\nData in Linked List-1:");
display(h1);//calling display fun by paasing h1 i.e base address of first node to print first linked list elements
printf("\n");
printf("\nLinked List-2:Enter No.of Nodes to be Inserted:");
scanf("%d",&total_second);
//logic to give first list elements
for(i=0;i<total_second;i++)
{
printf("Enter Data %d:",i);
scanf("%d",&second_list_value);
h2=insert(h2,second_list_value);//calling insert function passing arguments.
}
printf("\nData in Linked List-2:");
display(h2);//calling display fun by passing h2 i.e base address of first node to print second linked list elements
//calling union function by passing aruguments and assign the return head address to result_union variable.
result_union=fun_union(h1,h2);
printf("\n");
printf("\nUnion of 2 Linked Lists:");
display(result_union);//calling display function to print union list
printf("\n");
//calling intersection function by passing aruguments and assign the return head address to result_intersection variable.
result_intersection=fun_intersection(h1,h2);
printf("\nIntersection of 2 Linked Lists:");
display(result_intersection);//calling display function to print intersection list
return 0;
}
//function definition of Insert
struct node *insert(struct node*headnode,int n)
{
//dynamically allocating 12 bytes of Memory using malloc which returns void pointer of the starting address of that memory block.
//created a pointer variable newnode to store address that malloc returns
//as malloc is returning void pointer, we are typecasting it.
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=n;//assign value n in data field of newly created node
newnode->next=0;// zero in link field
if(headnode==0)
{
headnode=temp=newnode;// headnode will be newnode if headnode=0
}
else
{
temp->next=newnode;//if not zero then newnode will be assigned to temp->next and temp also updated with newnode
temp=newnode;
}
return headnode;//returns headnode
}
//function definition of Union
struct node *fun_union(struct node* head1, struct node* head2)//passing base address of h1 and h2
{
temp1=head1;
while(temp1!=NULL)
{
un=insert(un,temp1->data);
temp1=temp1->next;
}
while(head2!=NULL)
{
if(!compare(un,head2->data))//calls compare fun to compare un list and second list
{
un=insert(un,head2->data); //calling insert function by passing un and value of second list
}
head2=head2->next;
}
return un;
}
//function definition of Intersection
struct node *fun_intersection(struct node*head1, struct node*head2)
{
while(head1 != NULL)
{
if(compare(head2,head1->data))
{
intersection=insert(intersection,head1->data);
}
head1=head1->next;
}
return intersection;
}
//function definition of compare fuction
int compare(struct node *headnode, int n)
{
while (headnode != NULL) {
if (headnode->data == n)
{
return 1;
}
headnode = headnode->next;
}
return 0;
}
//function definition of Display
void display(struct node *head)
{
if(head==0)
{
printf("List is empty");
}
else
{
tail=head;
while(tail!=0)//traverse the list and print the elements until tail not equals to zero.
{
printf("%d ",tail->data);
tail=tail->next;
}
}
}