Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
/*
============================================================================
Name : SetProject.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* Defining structure for linked list */
struct node
{
int data;
struct node *next;
};
bool isPresent(struct node* checkNode, int data);
void insertIntoNode(struct node** passNode, struct node* tempNode, int new_data);
void printList(struct node* displayNode);
struct node* getUnionList(struct node* list1, struct node* list2);
struct node* getIntersection(struct node* list1, struct node* list2);
/* Function to check if the value in one list already present
in the second list to avoid duplicate insert */
bool isPresent(struct node* checkNode, int data)
{
while(checkNode!=NULL)
{
if(checkNode->data==data)
return 1;
else
checkNode=checkNode->next;
}
return 0;
}
/*Function to create a new list with value , logic added to prevent duplicate insert into each list*/
void insertIntoNode(struct node** passNode, struct node* tempNode, int new_data)
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));
//Checking the value is already present in the list, if present not added
if(!isPresent(tempNode,new_data))
{
newNode->data=new_data;
newNode->next=(*passNode);
(*passNode)=newNode;
}
}
/*Function to display the elements inside a list */
void printList(struct node* displayNode)
{
while (displayNode!=NULL)
{
printf("%d\n", displayNode->data);
displayNode=displayNode->next;
}
}
/*Function to find the union of 2 lists*/
struct node* getUnionList(struct node* list1, struct node* list2)
{
struct node* result=NULL;
struct node* temp1=list1;
struct node* temp2=list2;
while(temp1!=NULL)
{
insertIntoNode(&result,result,temp1->data);
temp1=temp1->next;
}
while (temp2!=NULL)
{
if(!isPresent(result,temp2->data))
insertIntoNode(&result,result, temp2->data);
temp2=temp2->next;
}
return result;
}
/*Function to find the intersection of 2 lists*/
struct node* getIntersection(struct node* list1, struct node* list2)
{
struct node* result=NULL;
struct node* temp=list1;
while(temp!=NULL)
{
if(isPresent(list2,temp->data))
insertIntoNode(&result,result,temp->data);
temp=temp->next;
}
return result;
}
int main(void) {
//puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
struct node* list1=NULL;
struct node* list2=NULL;
struct node* unionList=NULL;
struct node* interList=NULL;
insertIntoNode(&list1,list1,20);
insertIntoNode(&list1,list1,21);
insertIntoNode(&list1,list1,23);
insertIntoNode(&list1,list1,30);
insertIntoNode(&list1,list1,35);
printf("Displaying List1 \n");
printList(list1);
insertIntoNode(&list2,list2,15);
insertIntoNode(&list2,list2,16);
insertIntoNode(&list2,list2,16);
insertIntoNode(&list2,list2,12);
insertIntoNode(&list2,list2,19);
printf("Displaying List2 \n");
printList(list2);
unionList=getUnionList(list1, list2);
printf("Display of union list \n");
printList(unionList);
interList=getIntersection(list1, list2);
printf("Display of Intersection list \n");
printList(interList);
return EXIT_SUCCESS;
}