Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
OFFLINE
1.
#include <stdio.h>//preprocessor function
#include<stdlib.h>//preprocessor function
#include<stdbool.h>//preprocessor function
struct Node
{
int data;//structure member
struct Node *nextptr;//structure member
};
//inserting node
void push(struct Node **stunt, int new_data);//To insert mode at beginning of linked list
bool isPresent(struct Node *state, int data);//To check if given data is present in list
struct Node *getUnion(struct Node *state1,struct Node *state2)//To get Union of two list,State1,State2
{
struct Node *temp = NULL;//null used to no garbage value stored
struct Node *temp1 = state1, *temp2 = state2;
// Insert all elements of list1 to the result list
while (temp1 != NULL)//apply loop condition
{
push(&temp,temp1->data);
temp1 = temp1->nextptr;
}
// Insert those elements of list2 which are not
// present in result list
while (temp2 != NULL)//apply loop condition
{
if (!isPresent(temp,temp2->data))
push(&temp,temp2->data);
temp2 = temp2->nextptr;
}
return temp;
}
struct Node *getIntersection(struct Node* state1, struct Node* state2)//To get Intersection of two list,State1,State2
{
struct Node* temp = NULL;//null used to no garbage value stored
struct Node* temp1=state1;
while(temp1 != NULL)
{
if(isPresent(state2,temp1->data))
push(&temp,temp1->data);
temp1=temp1->nextptr;
}
return temp;
}
void push(struct Node **stunt, int new_data)
{
struct Node* model = (struct Node*)malloc(sizeof(struct Node));
model ->data = new_data;
model -> nextptr = *stunt;
*stunt= model ;
}
void pro(struct Node* rom)
{
while(rom != NULL)
{
printf("%d ", rom -> data);
rom = rom -> nextptr;
}
}
bool isPresent(struct Node* state, int data)
{
struct Node* t=state;
while(t !=NULL)
{
if(t->data == data)
return 1;
t=t->nextptr;
}
return 0;
}
int main()//main function
{
struct Node* state1=NULL;//Start with Empty list
struct Node* state2=NULL;//Start with Empty list
struct Node* Union=NULL;//Start with Empty list
struct Node* Intersection=NULL;//Start with Empty list
push(& state1 , 3);//create the linked list of state 1
push(& state1 , 4);//create the linked list of state 1
push(& state1 , 7);//create the linked list of state 1
push(& state1 , 8);//create the linked list of state 1
push(& state1 , 9);//create the linked list of state 1
push(& state2 , 3);//create the linked list of state 2
push(& state2 , 8);//create the linked list of state 2
push(& state2 , 5);//create the linked list of state 2
push(& state2 , 1);//create the linked list of state 2
push(& state2 , 2);//create the linked list of state 2
Union= getUnion(state1,state2);
Intersection = getIntersection(state1,state2);
printf("\n First Set is : ");//print the state 1
pro(state1);
printf("\n");
printf("\n Second Set is : ");//print the state 2
pro(state2);
printf("\n");
printf("\n Union is : ");//print the union
pro(Union);
printf("\n");
printf("\n Intersection is : ");//print the Intersection
pro(Intersection);
printf("\n");
return 0;
}