Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
- In this project set theory i.e., union and intersection is implemented with the help of linked list.
- 2 sets: set A and set B is created using linked list to enter the data
- 4 functions that are used in this project, they are 1)create linked list, 2)display the list entered, 3)finding union and 4)intersection of the given data
- This project is implanted with the help of Make file
- all necessary files are attached below
main.c:
#include"createnodelist.h"
#include"displaylist.h"
#include"findunion.h"
#include"intersection.h"
struct node{
int num;//data of the node//
struct node *nextptr;//address of the next node//
}*stnode[2];//structure pointer array is created to enter data in set A and set B
int arr1[100],arr2[100],n,m;//global variable declaration//
int main()
{
printf("\n application of linked list to create and display Union and Intersection of given sets\n");
printf("------------------------------------------------------------------------------------------");
printf("\n enter the size of set A");
fflush(stdout);
scanf("%d",&n);
printf("enter the numerical values in ascending order");
createnodelist(n);//calling function to create node list for set A
printf("\n enter the size of set B");
fflush(stdout);
scanf("%d",&m);
printf("enter the numerical values in ascending order");
createnodelist(m);//calling function to create node list for set B
printf("\n data entered in set A:");
displaylist();//calling function to display the entered data
for(int i=0;i<n;i++)
{
printf("%d,",arr1[i]);
}
displaylist();//calling function to display the entered data
printf("\n data entered in set B:");
for(int j=0;j<m;j++)
{
printf("%d,",arr2[j]);
}
printf("\n union of given sets :");
findunion(arr1,n,arr2,m);//calling function to find union of given sets
printf("\n Intersection of given sets :");
intersection(arr1,n,arr2,m);//calling function to find intersection of given sets
return 0;
}
createnodelist.c:
#include"createnodelist.h"
#include"displaylist.h"
#include"findunion.h"
#include"intersection.h"
struct node{
int num;//data of the node//
struct node *nextptr;//address of the next node//
}*stnode[2];//structure pointer array is created to enter data in set A and set B
int arr1[100],arr2[100],n,m;//global variable declaration//
void createnodelist(int z)
{
static int a=0;
for(int o=0;o<2;o++)
{
struct node *fnode,*temp;
int num,i;
stnode[a]=(struct node*)malloc(sizeof(struct node));
if(stnode[a]==NULL)
{
printf("\n memory cannot be allocated ");
}
else//reads the data for the node through keyboard//
{
printf("\n input data for set: ");
fflush(stdout);
scanf("%d",&num);
stnode[a]->num=num;
stnode[a]->nextptr=NULL;//links the address field to null//
temp=stnode[a];
//creating n nodes and adding to linked list//
for(i=2;i<=z;i++)
{
fnode=(struct node*)malloc(sizeof(struct node));
if(fnode==NULL)
{
printf("\n memory cannot be allocated ");
break;
}
else
{
printf("\n input data for set:");
fflush(stdout);
scanf("%d",&num);
fnode->num=num;//links the num filed of fnode with num
fnode->nextptr=NULL;//links the address field of fnode with NULL
temp->nextptr=fnode;//links previous node i.e. tmp to the field
temp=temp->nextptr;
}
}
}
a++;
break;
}
}
createnodelist.h:
#ifndef CREATENODELIST_H_
#define CREATENODELIST_H_
#include<stdio.h>
#include<stdlib.h>
void createnodelist(int n);//function to create list//
void displaylist();//function to display the list//
void findunion(int arr1[],int n,int arr2[],int m);//function declaration to find union//
void intersection(int arr1[],int n,int arr2[],int m);//function declaration to find intersection//
#endif /* CREATENODELIST_H_ */
displaylist.c:
#include"createnodelist.h"
#include"displaylist.h"
#include"findunion.h"
#include"intersection.h"
struct node{
int num;//data of the node//
struct node *nextptr;//address of the next node//
}*stnode[2];//structure pointer array is created to enter data in set A and set B
int arr1[100],arr2[100],n,m;//global variable declaration//
void displaylist()
{
static int b=0;
int i=0,j=0;
for(int p=0;p<2;p++)
{
struct node *temp;
if(stnode[b]==NULL)
{
printf(" \n List is empty ");
}
else
{
temp=stnode[b];
while(temp!=NULL)
{
//printf("\n data=%d ",temp->num);//Printed the data of current node
if(b==0)
{
arr1[i]=temp->num;//passing data into 1st array
i++;
}
if(b==1)
{
arr2[j]=temp->num;//passing data into 2nd array
j++;
}
temp=temp->nextptr;//advance the position of current node
}
}
b++;
break;
}
}
displaylist.h:
#ifndef DISPLAYLIST_H_
#define DISPLAYLIST_H_
#include<stdio.h>
#include<stdlib.h>
void createnodelist(int n);//function to create list//
void displaylist();//function to display the list//
void findunion(int arr1[],int n,int arr2[],int m);//function declaration to find union//
void intersection(int arr1[],int n,int arr2[],int m);//function declaration to find intersection//
#endif /* DISPLAYLIST_H_ */
findunion.c:
#include"createnodelist.h"
#include"displaylist.h"
#include"findunion.h"
#include"intersection.h"
struct node{
int num;//data of the node//
struct node *nextptr;//address of the next node//
}*stnode[2];//structure pointer array is created to enter data in set A and set B
int arr1[100],arr2[100],n,m;//global variable declaration//
void findunion(int arr1[],int n,int arr2[],int m)//function definition for finding union of given sets
{
int k=0,l=0;//local variables declaration to access array1 and array2
while((k<n)&&(l<m))
{
if(arr1[k] < arr2[l])
{
printf("%d,",arr1[k++]);
}
else if(arr2[l] < arr1[k])
{
printf("%d,",arr2[l++]);
}
else
{
printf("%d,",arr1[k++]);
l++;
}
}
while(k<n)
{
printf("%d,",arr1[k++]);
}
while(l<m)
{
printf("%d,",arr2[l++]);
}
}
findunion.h:
#ifndef FINDUNION_H_
#define FINDUNION_H_
#include<stdio.h>
#include<stdlib.h>
void createnodelist(int n);//function to create list//
void displaylist();//function to display the list//
void findunion(int arr1[],int n,int arr2[],int m);//function declaration to find union//
void intersection(int arr1[],int n,int arr2[],int m);//function declaration to find intersection//
#endif /* FINDUNION_H_ */
intersection.c:
#include"createnodelist.h"
#include"displaylist.h"
#include"findunion.h"
#include"intersection.h"
struct node{
int num;//data of the node//
struct node *nextptr;//address of the next node//
}*stnode[2];//structure pointer array is created to enter data in set A and set B
int arr1[100],arr2[100],n,m;//global variable declaration//
void intersection(int arr1[],int n,int arr2[],int m)//function definition to find intersection
{
int k=0,l=0;//local variables declaration to access array1 and array2
while((k<n) && (l<m))
{
if(arr1[k]<arr2[l])
k++;
else if((arr2[l])<arr1[k])
l++;
else
{
printf("%d,",arr2[l]);
k++;
l++;
}
}
}
intersection.h:
#ifndef INTERSECTION_H_
#define INTERSECTION_H_
#include<stdio.h>
#include<stdlib.h>
void createnodelist(int n);//function to create list//
void displaylist();//function to display the list//
void findunion(int arr1[],int n,int arr2[],int m);//function declaration to find union//
void intersection(int arr1[],int n,int arr2[],int m);//function declaration to find intersection//
#endif /* INTERSECTION_H_ */
Makefile:
all:main.c createnodelist.c createnodelist.h displaylist.c displaylist.h findunion.c findunion.h intersection.c intersection.h
gcc main.c createnodelist.c createnodelist.h displaylist.c displaylist.h findunion.c findunion.h intersection.c intersection.h -o settheory -I.