Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
#include <stdio.h>
#include <stdlib.h>
/*linked list structure*/
struct node
{
int data;
struct node *next;
} *ekk,*doo,*uniion,*inter;
void initialize()
{
ekk = doo = NULL;
}
//insert fun
void insert(struct node **head, int num)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node)); //adding new node from user
newnode->data = num; //adding new node
newnode->next = *head; //pointing to new data to head
*head = newnode; //new node is head
}
//searching element in list
int search (struct node *head, int num)
{
while (head !=NULL)
{
if (head ->data ==num)
{
return 1;
}
head = head->next;
}
return 0;
}
//union condition
struct node* findunion(struct node *ekk, struct node *doo)
{
uniion = NULL;
struct node *temp = ekk; //adding all node from ekk to union
while(temp !=NULL)
{
insert (&uniion, temp->data);
temp = temp->next;
}
while (doo != NULL) //adding uncommon data from doo to union
{
if (!search(ekk,doo->data))
{
insert(&uniion,doo->data);
}
doo = doo->next;
}
return uniion;
}
//intersection condition
struct node* intersection(struct node *ekk, struct node *doo)
{
inter = NULL;
while (ekk != NULL)
{
if (search(doo, ekk->data))
{
insert(&inter, ekk->data);
}
ekk = ekk->next;
}
return inter;
}
void printlist(struct node *nodePtr) //printing lists statement
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf(",");
}
}
int main()
{
int i, ekkcount, doocount,temp;
initialize();
printf("\nEnter number of nodes for first list: "); //first list
fflush (stdout);
scanf("%d",&ekkcount);
printf("\nEnter %d data for first list: ",ekkcount);
for(i=0; i<ekkcount; i++)
{
fflush (stdout);
scanf("%d",&temp);
insert(&ekk,temp);
}
printlist(ekk);
printf("\nEnter number for second list: "); //second list
fflush(stdout);
scanf("%d",&doocount);
printf("\nEnter %d data for second list: ",doocount);
for(i=0; i<doocount;i++)
{
fflush(stdout);
scanf("%d",&temp);
insert(&doo,temp);
}
printlist(doo);
//printing union and intersection
findunion(ekk,doo);
intersection(ekk,doo);
printf("\nUnion is ");
printlist(uniion);
printf("\nIntersection is ");
printlist(inter);
return 0;
}