Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
*---OFF LINE---*
UNION AND INTERSECTION PROJECT :-
#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 **magizh, int new_data);//To insert mode at beginning of linked list
bool isPresent(struct Node *station, int data);//To check if given data is present in list
struct Node *getUnion(struct Node *station1,struct Node *station2)//To get Union of two list,Station1,Sataion2
{
struct Node *temp = NULL;//null used to no garbage value stored
struct Node *temp1 = station1, *temp2 = station2;
// 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* station1, struct Node* station2)
{
struct Node* temp = NULL;
struct Node* temp1=station1;
while(temp1 != NULL)
{
if(isPresent(station2,temp1->data))
push(&temp,temp1->data);
temp1=temp1->nextptr;
}
return temp;
}
void push(struct Node **magizh, int new_data)
{
struct Node* stage = (struct Node*)malloc(sizeof(struct Node));
stage->data = new_data;
stage -> nextptr = *magizh;
*magizh=stage;
}
void printList(struct Node* ruso)
{
while(ruso != NULL)
{
printf("%d ", ruso -> data);
ruso = ruso -> nextptr;
}
}
bool isPresent(struct Node* station, int data)
{
struct Node* t=station;
while(t !=NULL)
{
if(t->data == data)
return 1;
t=t->nextptr;
}
return 0;
}
int main()//main function
{
struct Node* station1=NULL;//Start with Empty list
struct Node* station2=NULL;//Start with Empty list
struct Node* Union=NULL;//Start with Empty list
struct Node* Intersection=NULL;//Start with Empty list
push(& station1 , 5);//create the linked list of station 1
push(& station1 , 4);//create the linked list of station 1
push(& station1 , 7);//create the linked list of station 1
push(& station1 , 3);//create the linked list of station 1
push(& station1 , 9);//create the linked list of station 1
push(& station2 , 3);//create the linked list of station 2
push(& station2 , 9);//create the linked list of station 2
push(& station2 , 5);//create the linked list of station 2
push(& station2 , 8);//create the linked list of station 2
push(& station2 , 2);//create the linked list of station 2
Union= getUnion(station1,station2);
Intersection = getIntersection(station1,station2);
printf("\n First Line is : ");//print the station 1
printList(station1);
printf("\n");
printf("\n Second Line is : ");//print the station 2
printList(station2);
printf("\n");
printf("\n Union is : ");//print the union
printList(Union);
printf("\n");
printf("\n Intersection is : ");//print the Intersection
printList(Intersection);
printf("\n");
return 0;
}