Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
//header files
# include <stdio.h>
# include <stdlib.h>
//structure declaration of node
struct node
{
int data;
struct node *next;
};
//structure object declaration
struct node *head,*newnode,*temp;
//function prototype
struct node *createnode(int);//function to create a new node and insert value
struct node * insert(struct node *,int value); //function to link nodes of the list
void display(struct node *); //function to display the elements entered into the list
int ispresent(struct node *,int); //function to find whether an element is present in the list
struct node *findunion(struct node *,struct node *);//function to find union of two lists
struct node *findintersection(struct node *,struct node *);//function to find intersection of two lists
//function to create a new node
struct node *createnode(int value)
{
newnode=(struct node *)malloc(sizeof(struct node)); //dynamic memory allocation using malloc
if(newnode==0) //condition to check whether memory allocation is successful
printf("Memory not allocated");
else
{
newnode->data=value; //assigning the entered value to dynamically allocated memory
newnode->next=NULL;
}
return newnode;
}
//function definition to insert elements entered by the user to the list
struct node * insert(struct node * newnode,int value)
{
newnode=createnode(value);
if(head==0)
head=temp=newnode; //assigning the beginning of the list to head
else
{
temp->next=newnode; //linking the added nodes in list
temp=newnode;
}
return head;
}
//function to display the elements entered into the list
void display(struct node * list)
{
while(list!=NULL)
{
printf("%d\t",list->data);
list=list->next;
}
}
//function to find whether an element is present in the list
int ispresent(struct node * list,int value)
{
while(list!=0)
{
if(list->data==value)
{
return 1;
}
list=list->next;
}
return 0;
}
struct node *findunion(struct node *list1,struct node *list2)
{
struct node *output,*curr1,*curr2; //local variable declaration
output=NULL;
curr1=list1;
curr2=list2;
while(curr1!=NULL)
{
if(!ispresent(output,curr1->data))
output=insert(output,curr1->data);//function to insert the unique elements of list 1
curr1=curr1->next;
}
while(curr2!=NULL)
{
if(!ispresent(output,curr2->data))
output=insert(output,curr2->data);//function to insert the unique elements of list 2
curr2=curr2->next;
}
return output;
}
//function to find intersection of two lists
struct node *findintersection(struct node *list1,struct node *list2)
{
struct node *output,*curr1,*curr2; //local variable declaration
output=NULL;
curr1=list1;
curr2=list2;
while(curr2!=NULL)
{
if((ispresent(curr1,curr2->data))&&(!ispresent(output,curr2->data))) //function to find commom elements in both lists
output=insert(output,curr2->data);
curr2=curr2->next;
}
return output;
}
int main()
{
//declaration of local variables and objects of structure
struct node *list1,*list2,*unionlist,*intersectionlist;
int m,n,value1=0,value2=0;
//to enter and insert elements of list 1
printf("*****************LIST 1*********************\n");
printf("\nEnter the number of elements of list 1 : ");
fflush(stdout);
scanf("%d",&m);
for(int i=0;i<m;i++)
{
printf("\nEnter element %d :",i+1);
fflush(stdout);
scanf("%d",&value1);
list1=insert(list1,value1); //function to insert elements entered by the user to the list
}
head=NULL;
//to enter and insert elements of list 2
printf("\n*****************LIST 2*********************\n");
printf("\nEnter the number of elements of list 2 : ");
fflush(stdout);
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\nEnter element %d :",i+1);
fflush(stdout);
scanf("%d",&value2);
list2=insert(list2,value2); //function to insert elements entered by the user to the list
}
head=NULL;
printf("\n****************************OUTPUT*********************************\n");
//to display both lists
printf("\nThe values in list 1 are : ");
display(list1);
printf("\nThe values in list 2 are : ");
display(list2);
//to find and display union of two lists
unionlist=findunion(list1,list2);
printf("\nThe union of lists is : ");
display(unionlist);
head=NULL;
//to find and display intersection of two lists
intersectionlist=findintersection(list1,list2);
printf("\nThe intersection of lists is : ");
display(intersectionlist);
free(newnode);
return 0;
}